In the language c using the isspace() function: Write a program to count the num
ID: 3800582 • Letter: I
Question
In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters.
Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters.
Test the program by using the statement below
You're traveling through
another dimension;
a dimension not only
of sight and sound,
but of mind.
Explanation / Answer
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char ch, file_name[25];
FILE *fp;
int no_of_characters=0;
int no_of_lines=0;
int no_of_words=0;
char check;
printf("Enter the path/name of file you wish to see ");
gets(file_name);
fp = fopen(filename,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file. ");
exit(EXIT_FAILURE);
}
if( ( ch = fgetc(fp) ) != EOF ){
ch=fgetc(fp);
no_of_characters++;
check=ch;
}
while( ( ch = fgetc(fp) ) != EOF ){
if(!(check==' ') && !(check==' '))
no_of_characters++;
if(isspace(ch) && !(check==' ') && !(check==' ')){
no_of_words++;
}
if(ch == ' '){
no_of_lines++;
}
check=ch;
}
if(no_of_characters > 0){
no_of_words++;
}
if(no_of_characters > 0 && no_of_lines < 1){
no_of_lines++;
}
fclose(fp);
printf("The no of characters are : %d ",no_of_characters);
printf("the no of the words are : %d ",no_of_words);
printf("The no of lines are : %d ",no_of_lines);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.