Question

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 sentence \n");
fgets(sentence, 100, stdin);

for (i = 0; sentence[i] != '\0'; i++)
{
if (sentence[i] == ' ')
{
count ++;
}
}
printf("You entered %d words\n", count);

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

void main(void)
{
char sentence[100];
int i = 0;
int count = 1;


printf("Enter a sentence \n");
fgets(sentence, 100, stdin);

for (i = 0; sentence[i] != '\0'; i++)
{
if (sentence[i] == ' ')
{
count ++;
}
else if(sentence[i] == 'd' && sentence[i] == '-')
{
break;
}
}
printf("You entered %d words\n", count-1);

return 0;
}

F:ACPP programslwordslbin\Debuglwords.exe Enter a sentence assdf hju ghj -d You entered 3 words Process returned 20 (ex14) x

Add a comment
Know the answer?
Add Answer to:
The program reads an unknown number of words – strings that all 20 characters or less...
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
  • I need this written in the C format not C++ Name this program count.c-The program reads...

    I need this written in the C format not C++ Name this program count.c-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. 4.

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • the program will ask the user to enter 5 strings then the program will determine how...

    the program will ask the user to enter 5 strings then the program will determine how many A's are the collection of strings. #include<stdio.h> #include<string.h> int main(void){ int i,j,count =0; char words[5][30] for(i=0;i<5;i++) scanf("[1]", [2] ); for(i=0;i<5;i++) for(j=0;j< [3]; j++) if( [4] =='A') count ++; printf("there are %dA's.,count); return 0 } which expressions should replace [1]? %s,%c,%d,%f which expressions should replace [2]? words[i],&words[i],words[i][0],&words[i][0] which expressions should replace [3]? strlen(words[i]),words[i],words[i][j],length.words[i] which expressions should replace [4] words[i][j],words[j][i],words[i],words[i][0]

  • /* * This program reads characters from stdin and writes them back to stdout. * Student...

    /* * This program reads characters from stdin and writes them back to stdout. * Student task: change code so that output is rotated output one position * to the left. Thus, if input is "abcd", output should be "bcda". #include <stdio.h> #define BUFFER_SIZE 81 int main(int argc, char **argv) { char string[BUFFER_SIZE]; while(fgets(string, BUFFER_SIZE, stdin) > 0) { int numChars = 0; while(string[numChars] && string[numChars] != '\n') ++numChars; int i; for(i = 0; i < numChars; ++i) putchar(string[i]); putchar('\n');...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • 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...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • C PROGRAM The following is code prints the current activation record number, the memory address of...

    C PROGRAM The following is code prints the current activation record number, the memory address of the current array, followed by the estimated size of the current activation record as a distance between the current array address and the array address from the previous activation record. I need it to run until a segmentation fault occurs and also it must print the estimated size of the runtime stack as a product of the size of current activation record and the...

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