Thursday, September 13, 2012

Capitalize First Letter Of Sentences

Compiler Used: TurboC
Language: C

This program demonstrates how to read file contents from c.txt, capitalize first letter of every sentence and then write it to d.txt.


//capitalize first letter of every sentence
//after every fullstop capitalize the first alphabet encountered
#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int cap=1;
char c;
FILE *fp1, *fp2;

fp1 = fopen("c.txt", "r");
fp2 = fopen("d.txt", "w");
while((c=getc(fp1))!=EOF){
if(c == '.'){
cap =1;
}
if(cap ==1 && c >=97 && c<=122){
c = c-32;
cap =0;
}
putc(c, fp2);
}
rewind(fp1);
while((c=getc(fp1))!=EOF){
 putchar(c);
}
printf("\n\n");
fclose(fp1);
fclose(fp2);
fp2 = fopen("d.txt", "r");
while((c=getc(fp2))!=EOF){
    putchar(c);
}
getch();
}

Here is what output looks like:

You can see contents of c.txt has been copied to d.txt and first letter of every sentence has been capitalized.

PS: Improvements/Suggestions/Comments on the program are welcome. I am sure there are better ways to do it.

No comments:

Post a Comment