句子之间有两个空格

时间:2013-02-17 20:16:17

标签: c

我有一个代码,用于查找单词之间是否有多个空格,在这种情况下,将它们更改为一个空格。 我需要添加一些额外的功能,它应该在句子之间留出两个空格 (句子的最后一个符号是。)

例如。

如果我有带文字的文件:

This     is my    first program.        Hello     world

程序应该打印我:

This is my first program.  Hello world

代码:

# include <stdio.h>
# include <stdlib.h>

int main()
{
FILE *in;
char myStr[100],newStr[100];
int ch;
int j,i,k,z=0;

in=fopen("duom.txt","r");

if(in){
while(EOF != ch){
ch=fgetc(in);
myStr[z] = ch;
z++;
k=0;
for(i=0; myStr[i] != '\0'; i++) {     
    if(myStr[i-1] != '.' && myStr[i] == ' ' && myStr[i+1] == ' ' ) 
      continue;          
   newStr[k]= myStr[i]; 
   k++;      
}   
}
}

for(j=0;j<k;j++){       

     printf("%c",newStr[j]);                               
    }
printf("\n");

fclose(in);

system("pause");
return 0;
}

我不要求你写我的整个代码,只是给我一些想法。

抱歉我的英文不好:/

2 个答案:

答案 0 :(得分:5)

此循环遵循以块为单位处理文件的一般方法:

您的方法修订:

# include <stdio.h>
# include <stdlib.h>

int main() {
  FILE *in;
  char myStr[100],newStr[100];
  int ch;
  int j,i,k,z=0;

  in=fopen("duom.txt","r");

  if(!(in)) { fprintf(stderr,"Error opening file!\n"); }
  else {        //the file was opened
    int go = 1; //master loop control
    while(go) { //master loop
      z  = 0;   //set sub loop
      ch = '\0';//control variables
      while(z < 100 && EOF != ch){ //process file in 99 character blocks
        ch=fgetc(in);              //getting one character at a time 
        if(EOF == ch) { go = 0; }  //break master loop
        else { myStr[z++] = ch; }  //or process char
      }
      myStr[z] = '\0';             //null terminate the string
      for(i=0; myStr[i] != '\0'; i++) {
        //i=99='\0' <-- assumed is highest string size

        //if i=0; Do you really want that leading space?
        if(i== 0 && myStr[i] == ' ' ) { continue; }

        //if i=98 it is the last char in the string i=99 should be '\0'
        //So do you really want that trailing space?
        if(i==98 && myStr[i] == ' ' ) { continue; }

        //Same rational as above.
        //So do you really want those trailing 2 spaces?
        if(i==97 && myStr[i] == ' ' && myStr[i+1] == ' ') { continue; }

        //if i=0; myStr[i-1] will likely cause a segmentation fault
        if(i > 0 && myStr[i] == ' ' && myStr[i+1] == ' ' && myStr[i-1] != '.') { continue; }
        newStr[k] = myStr[i]; 
        k++;      
      }
      for(j=0;j<k;j++){ printf("%c",newStr[j]); } //print the 99 char block
    }
    printf("\n"); //print a newline for good measure
    fclose(in);   //close file
  }
  return 0;
}

请注意,对于大小超过99个字符的文件,代码会出错,因为间距格式比较不是从一个99字符块的末尾到另一个字符串的开头。你可以通过不删除比较i = 1&amp;的值的前导/尾随空格来实现这一点。 i = 2,最后两个字符在i = 97&amp;在前一个区块中我= 98。

这是一个不同的,更好的循环。它解决了另一种方法的阻塞问题,并且使用了更少的内存

更好的方法:

# include <stdio.h>
# include <stdlib.h>
int main() {
  FILE *in;
  in=fopen("duom.txt","r");

  if(!(in)) { fprintf(stderr,"Error opening file!\n"); return -1; }
           //the file was opened
  int x; //stores current  char
  int y; //stores previous char
  for(y='\0'; (x=fgetc(in)) != EOF; y=x) { //read in 'x' until end of file
// The following conditions cover all cases:
// is 'x' not a space? Then print 'x'
// is 'x' a space but 'y' a period? Then print two spaces
// is 'x' a space and 'y' not a period but also not a space? Then print a space
// Otherwise 'x' is part of extra spacing, do nothing
         if(x != ' ')                         { printf("%c",x); }
    else if(x == ' ' && y == '.')             { printf("  ");   }
    else if(x == ' ' && y != '.' && y != ' ') { printf(" ");    }
    else { ; }  //do nothing
  }   
  printf("\n"); //print a newline for good measure
  fclose(in);   //close file
  return 0;
}

答案 1 :(得分:2)

我建议使用strtok()并将标记连接在一起,用正确的空格数分隔。如果令牌以句点结尾,请使用两个空格。否则,只使用一个。这样你甚至不需要检查单词之间有多少空格。

相关问题