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, so there is 3 spaces after the e ), then the output should be

a= [She], b=3

a= [ate], b=3

a= [ ], b=-1

a= [ ], b=-1

a= [ ], b=-1

a= [ ], b=0000

Will need to print a= [ ], b=[-1] for each space if there is any.  

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 there are multiple newlines in a string that you will need to print out a= [ ], b=1000 for every new line.

If the case sensitive 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

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

So if a string contains the word "stopper" it will not print out a= [stop], b=0000. It would print out a= [stopper], b=7.

Please use these strings given and show output. Thank you.

Solutions

Expert Solution

//do comment if any problem arises
//code
#include <stdio.h>
#include <string.h>
//this function prints a and b
void print(char current_word[], int l)
{
//if no word
if (l == 0)
return;
if (strcmp(current_word, "stop") == 0)
printf("a= [%s], b=0000\n", current_word);
else
printf("a= [%s], b=%d\n", current_word, l);
}
//this number of characters prints frequency of each word
void printWords(char string[])
{
char current_word[100];
int l = 0, i = 0;
int is_start = 0;
while (1)
{
//if a space after starting of string
if (string[i] == '-')
{
if (is_start)
{
if (l != 0)
{
print(current_word, l);
current_word[0] = '\0';
l = 0;
}
printf("a= [ ], b=-1\n");
}
}
//if EOF
else if (string[i] == '\0')
{
//print current word
print(current_word, l);
l = 0;
//now print endl
current_word[l++] = string[i];
printf("a= [%s], b=0000\n", current_word);
return;
}
//if newline character
else if (string[i] == '\n')
{
print(current_word, l);
l = 0;
current_word[l++] = string[i];
printf("a= [%s], b=1000\n", current_word);
return;
}
//if space
else if (string[i] == ' ')
{
print(current_word, l);
l = 0;
current_word[l] = '\n';
}
else
{
is_start = 1;
current_word[l++] = string[i];
current_word[l] = '\0';
}

i++;
}
}
int main()
{
char string[1000];
printf("Enter the string: ");
gets(string);
printWords(string);
}

Output:


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...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The words that need to be found are in the file 'WordList.txt'. There are 100 words to be found. You must find the location of the first and last letter of each word as well as the cardinal direction that describes the word's orientation. The location of first and last letters are to be described in terms of row and column, with indexing starting at...
Make a python dictionary that counts the number of times each word occurs in the the...
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt'
Make a python dictionary that counts the number of times each word occurs in the the...
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt' without downloading to computer.
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT