Question
c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?.



about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from sta
include<stdio . h> 2 #include<stdlib . h> 3 4 int main(){ 6 unsigned long int charcount=o; unsigned long int wordcount= ; uns
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

Try this code

#include <stdio.h>
int main( )
{

unsigned long int charcount=0;
   unsigned long int wordcount=0;
  unsigned long int linecount=0;
int word=1;
char input;
while( (input=getchar())!= EOF )
{
   charcount=charcount+1;
   if (input=='\n')
   {
       linecount=linecount+1;
   word=1;
   }
  
   else if ((input>='a' && input<='z')||(input>='A' && input<='Z')||input=='\'')
   {
      
       if (word==1)
       {
           wordcount=wordcount+1;
           word=0;
       }
      
   }
       else
       {
           word=1;
       }
  
}  
printf( "%lu %lu %lu\n", charcount, wordcount, linecount );
return 0;       
}      

--

all the best

Add a comment
Know the answer?
Add Answer to:
c program that counts the number of characters, words and lines from standard input until EOF....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

    What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long digitCount=0; long lineCount=0; long wordCount=0; long digitFreq[10]; int c=0; int outOfWord=0; int state=0; int inWord=1; void printStats(); void totalWords(); void totalLines(); void digitFrequency(); int main(int argc,char **argv) { int i; for(i=0;i<10;i++) { digitFreq[i]=0; } state=outOfWord; c=getchar(); while((c !=EOF)) { charCount++; digitFrequency(c); totalLines(c); totalWords(c); c=getchar(); } printStats(); return 1; } void totalLines(int c) { if(c == '\n') { lineCount++; } } void digitFrequency(int c) {...

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Make a C program to count the number of occurrences of words from the input. For...

    Make a C program to count the number of occurrences of words from the input. For example, with input "one two one three one two" your program should output: one 3 two 2 three 1 It should work for up to 100 different words. If there are more than 100 unique words in the input, the program should still work, and count the number of appearances of the first 100 unique words. Each word should have the same maximum amount...

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • I am writing a program that reads ten integers from standard input, and determines if they...

    I am writing a program that reads ten integers from standard input, and determines if they are going up or down or neither. Can I get some help? I have supplied what I have already. #include <stdio.h> #include <string.h> #include <stdlib.h> int isGoingUp(int [a]); int isGoingDown(int [b]); int main(void) { int array[10]; printf("Enter ten integers : "); for (int a = 0; a < 10; a++) scanf("%d", &array[a]); if (isGoingUp(array) == 1) printf("The values are going up\n"); else if (isGoingDown(array)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT