c - Two spaces between sentences -
i have code finds if there more 1 space between words, in case alter them one. , need add together additional function should create 2 spaces between sentences. (a sentence's lastly symbol . )
for example.
if have file text:
first program. hello worldprogram should print me:
first program. hello worldcode:
# 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"); homecoming 0; }
i don't inquire write whole code, give me ideas.
sorry bad english language :/
this loop follows general approach of processing 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 opened int go = 1; //master loop command 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 1 character @ time if(eof == ch) { go = 0; } //break master loop else { mystr[z++] = ch; } //or process char } mystr[z] = '\0'; //null terminate string for(i=0; mystr[i] != '\0'; i++) { //i=99='\0' <-- assumed highest string size //if i=0; want leading space? if(i== 0 && mystr[i] == ' ' ) { continue; } //if i=98 lastly char in string i=99 should '\0' //so want trailing space? if(i==98 && mystr[i] == ' ' ) { continue; } //same rational above. //so want trailing 2 spaces? if(i==97 && mystr[i] == ' ' && mystr[i+1] == ' ') { continue; } //if i=0; mystr[i-1] cause 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 99 char block } printf("\n"); //print newline measure fclose(in); //close file } homecoming 0; }
note code misbehave files size greater 99 chars because spacing format comparisons not made end of 1 99 char block origin of another. implement not deleting leading/trailing spaces comparing values @ i=1 & i=2 lastly 2 chars @ i=97 & i=98 in previous block.
this different, improve loop. solves block barrier issues of other approach , 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"); homecoming -1; } //the file 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 // next conditions cover cases: // 'x' not space? print 'x' // 'x' space 'y' period? print 2 spaces // 'x' space , 'y' not period not space? print space // otherwise 'x' part of spacing, nil if(x != ' ') { printf("%c",x); } else if(x == ' ' && y == '.') { printf(" "); } else if(x == ' ' && y != '.' && y != ' ') { printf(" "); } else { ; } //do nil } printf("\n"); //print newline measure fclose(in); //close file homecoming 0; }
c
No comments:
Post a Comment