Question

In: Computer Science

In the language c using the isspace() function: Write a program to count the number of...

In the language c using the isspace() function: Write a program to count the number of words, lines, and characters when user enter statements as input. A word is any sequence of non-white-space characters.

Have the 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.

Example of the user input could be the statement below:

             You're traveling through

​               another dimension;

              a dimension not only

              of sight and sound,

               but of mind.

Solutions

Expert Solution

#include <stdio.h>
#include<stdlib.h>
int main()
{
char data[1000]; // variable to store the input
char ch; // char variable to take a input
int index = 0,charCount = 0, wordCount =0,lineCount = 1, i=0;
printf("Enter the Input ( hit ctrl + D(in linux) ctrl+Z(in windows) ):\n");
while(ch != EOF) // terminates if user hit ctrl + D(in linux) ctrl+Z(in windows)
{
ch = getchar();
data[index] = ch;
index++;
}
data[index] = '\0';// inserting null character at end

if(index==1){ // if nothing everyting counts to 0(zero)
charCount = 0;
wordCount =0;
lineCount =0;
}
// looping until index-1 times (-1 beacause last char is null, we need not to be considered)
else {
for(i=0;i<index-1;i++){
charCount++;
if(data[i] == '\n'){
lineCount++;
}
  
else if(isspace(data[i]) || data[i] == '\n'){ // current char is space
if((!isspace(data[i-1]) && i !=0)){ // previous char is not char (to elemenate multiple space condition)
wordCount++;
}
}
}
if(!isspace(data[index-3])){ // if last char is not space then we need to add wordcount
wordCount++;
}
}
  
printf("\nline Count : %d \nword Count : %d \nchar Count : %d \n",lineCount,wordCount,charCount);
return 0;
}

ScreenShot:


Related Solutions

Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Q3)   Write a C program that counts the number of odd numbers with using function count()...
Q3)   Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
write a program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
>>>>>C#<<<<< A.     Number Frequency-Arrays Write a program to count the number of 10’s in the given...
>>>>>C#<<<<< A.     Number Frequency-Arrays Write a program to count the number of 10’s in the given list of 15 numbers. The output should look like:                                                                                                        [01] Extend your program to work with a list of n numbers.                                         [01]                                                                                              Random Numbers-Methods Write a program to generate a 10 Random Numbers:                                           [01] Extend your program to generate n Random numbers.                                         [01] B.    Word Count -String Processing Write a program to count the occurrence...
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
In C language Write a program that includes a function search() that finds the index of...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT