Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two spaces between sentences

Tags:

c

I have a code which finds if there is more than one space between the words, in that case change them to one. And I need to add some additional function which should make two spaces between sentences.
(A sentence's last symbol is . )

For example.

if i have file with text:

This     is my    first program.        Hello     world

program should print me:

This is my first program.  Hello world

Code:

# 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;
}

I don't ask you to write my whole code, just give me some ideas.

Sorry for my bad english :/

like image 482
Lukas M. Avatar asked Feb 17 '13 20:02

Lukas M.


2 Answers

This loop follows your general approach of processing the file in blocks:

Your Approach Revised:

# 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;
}

Note that the code will misbehave for files with size greater then 99 chars because spacing format comparisons are not be made from the end of one 99 char block to the beginning of another. You could implement this by not deleting the leading/trailing spaces comparing the values at i=1 & i=2 with the last two chars at i=97 & i=98 in the previous block.

This is a different, better loop. It solves the block barrier issues of the other approach and uses much less memory

Better approach:

# 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;
}
like image 179
12 revs Avatar answered Oct 08 '22 06:10

12 revs


I suggest using strtok() and concatenate the tokens together separated by the correct number of spaces. If a token ends with a period, use two spaces. Otherwise, only use one. This way you don't even need to check how many spaces are in between words.

like image 24
Code-Apprentice Avatar answered Oct 08 '22 04:10

Code-Apprentice