heres the logic. take a look and try to understand what is happening.
Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int sentence = 0;
int words = 0;
int avgwords = 0;
int cap_flag = 1;
FILE *infile = fopen("input.txt", "r");
FILE *outfile = fopen("output.txt", "w");
if(infile != NULL){
while(c != EOF){
c = fgetc(infile);
if(c == ' '){
words++;
}
if(c == '.' || c == '?' || c == '!'){
sentence++;
cap_flag = 1;
fputc(c, outfile); //my butt fuck way around not trying to capp the period and space after the period lol
c = fgetc(infile);
fputc(c, outfile);
c = fgetc(infile);
}
if(cap_flag){
if((int)c >= (int)'a' && c <= (int)'z'){
c -= 32;
}
cap_flag = 0;
}
else{
if((int)c >= (int)'A' && (int)c <= (int)'Z'){
c += 32;
}
}
fputc(c, outfile);
printf("%c", c);
}
}
printf("\n\ntotal words = %d\n", words);
printf("total sentences = %d\n", sentence);
printf("average words per sentence = %d\n", words / sentence);
fclose(infile);
fclose(outfile);
return 0;
}
input
Code
ThIs Is sOME Test Data. We WilL Use this to Test Our aPliCaton. Why shAlL we Use This? BecauSe I Say So.
output
Code
This is some test data. We will use this to test our aplicaton. Why shall we use this? Because i say so.ÿÿ
This post was edited by AbDuCt on Jun 13 2012 12:38am