I try to write a program that count words and letters in a text that taken from a file that exist in harddrive. But this program just count number of letters of first line.what can I do? please give your idea about this program. please debug my code in your compiler and help me to improve it.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int letter(const char sentence[ ]);
int words(const char sentence[ ]);
int main(int argc, char *argv[]) {
char sentence[100];
const char *filename = "test.txt";
FILE *cfPtr;
if (argc == 2)
filename = argv[1];
if ( (cfPtr = fopen(filename, "r")) == NULL ) {
printf( "File '%s' could not be opened\n", filename );
}
else {
int total = 0;
int total2 =0;
while (fgets(sentence, sizeof sentence, cfPtr))
total += words(sentence);
total2 +=letter(sentence);
printf("%d\n", total2);
printf("%d\n", total);
printf("****%s",*cfPtr);
fclose(cfPtr);
}
getch();
return 0;
}
int words(const char sentence[ ])
{
int i, length=0, count=0, last=0;
length= strlen(sentence);
for (i=0; i<length; i++)
{
if (sentence[i] == ' '||sentence[i] == '\t'||sentence[i] == '\n')
count++;
}
return count;
}
int letter(const char sentence[ ])
{
int i, length=0, count=0;
length= strlen(sentence);
for (i=0; i<length; i++)
{
if ((sentence[i]>'a'&&sentence[i]>'z')||(sentence[i]>'A'&&sentence[i]>'Z'))
count++;
}
return count;
}
please debug my code in your compiler
We want to help you, we do not want to do your job. A better question would be "How can I debug this code?" ... – HAS Jun 21 '13 at 9:39(sentence[i]>'a'&&sentence[i]>'z')||(sentence[i]>'A'&&sentence[i]>'Z')
Wrong maybe – BLUEPIXY Jun 21 '13 at 9:43