Question

In: Computer Science

Create a program in C that counts the number of characters in a word when a...

Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be

a= [The], b=3

a= [dog], b=3

a= [ is], b=2

a= [good], b=4

a= [ ], b=0000

Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example above)

Don’t take leading spaces into consideration. If there is spaces at the end of a string, for example “She ate_” ( _ is a space after the e), then the output should be

a= [She], b=[3]

a= [ate], b=[3]

a= [ ], b=[-1]

a= [ ], b=[00000]

If a newline at the end of the string, then output 1000. If user input “My name is Joe” with a new line after Joe then output.

a= [My], b=[2]

a= [name], b=[4]

a= [is], b=[2]

a= [Joe], b=[3]

a= [ ], b=[1000]

a= [ ], b=[0000]            // End of file (EOF) is reached after the newline.

If the word “stop” is encountered in the string then print out 0000. If a user inputs “He said stop” then the output should be

a= [He], b=[2]

a= [said], b=[4]

a= [stop], b=[0000]

Solutions

Expert Solution

Hi, Please find the solution and rate the answer.

//

// main.c

// CountChars

//

//

#include <stdio.h>

void reinitialize(char *temp,int count){

for (int i=0; i<count; i++) {

temp[i] = ' ';

}

}

void printArray(char *temp){

for (int i=0; temp[i]!=' '; i++) {

if(temp[i]=='\n') break;

printf("%c",temp[i]);

}

}

int main(int argc, const char * argv[]) {

// insert code here...

printf("Enter the string");

char strr[100];

fgets(strr, sizeof strr, stdin);

int count=0;

int k=0;

char arrTemp[] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',};

int countFirstWord = 0;

for(int i = 0; strr[i] != '\0' && i<strlen(strr); ++i)

{

if(strr[i]==' ' && countFirstWord==0){//this is to skip leading spaces

continue;

}else{

countFirstWord=1;

}

if(strr[i]=='\n'){

printf("a=[],b=[1000]");

break;

if(strr[i+1]==EOF){

printf("a=[],b=[0000]\n");

break;

}

}

  

reinitialize(arrTemp, 20);

if(strr[i]!=' '){

int k=0;

for (k=0; k<20; k++) {

if(strr[i]==' ') break;

arrTemp[k]=strr[i++];

count++;

}

printf("a=[");

printArray(arrTemp);printf("], b=[");

  

printf("%d]\n",count);count=0;

  

}

  

  

}

return 0;

}

In the solution below, the leading spaces are removed using while loop until first word is encountered and at the end a new line is also counted.


Related Solutions

Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
Write a C program that counts the number of non white-space characters in an inputtext file....
Write a C program that counts the number of non white-space characters in an inputtext file. Program takes as command argument(s)the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writesthe output to an output file. The command format is as follows: command -f inputfile outputfile or, command -s inputfile -f indicates writing to an output file; -s indicates displaying the output on...
C++: A palindrome is a word, phrase, number, or other sequence of characters which reads the...
C++: A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon". Write a program using Stacks-Array and Queue-Array data structure we did in class to...
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.
2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT