Question

In: Computer Science

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 the main while loop that counts the total number of characters in a sentence, but can't get the program to count the individual vowels and the consonants. Could you comment the steps so I can figure out what I'm missing?

Solutions

Expert Solution

I WROTE THE CODE ALONG WITH THE COMMENTS:

CODE:

#include <iostream>

using namespace std;

int main()
{
//variables declaration.
char sentence[1000],ch;
int i=0,count_char=0,count_a=0,count_e=0,count_i=0,count_o=0,count_u=0,count_consonants=0;
  
cout<<"Enter sentence: ";
while(ch!='!' && ch!='.') //condition checking.
{
cin>>ch; //scan one by one character.
sentence[i]=ch;
i++;
  
if((ch>='a'&& ch<='z') || (ch>='A' && ch<='Z')) //condition for character
{
if(ch=='a' || ch=='A') //condition for a and A.
count_a++;
else if(ch=='e' || ch=='E') //condition for e or E.
count_e++;
else if(ch=='i' || ch=='I') //condition for i or I.
count_i++;
else if(ch=='o' || ch=='O') //condition for o or O.
count_o++;
else if(ch=='u' || ch=='U') //condition for u or U.
count_u++;
else //condition for consonants.
count_consonants++;
count_char++;
}

  
}
  
//display results.
cout<<"Number of characters: "<<count_char<<endl;
cout<<"Number of a's: "<<count_a<<endl;
cout<<"Number of e's: "<<count_e<<endl;
cout<<"Number of i's: "<<count_i<<endl;
cout<<"Number of o's: "<<count_o<<endl;
cout<<"Number of u's: "<<count_u<<endl;
cout<<"Number of consonants: "<<count_consonants<<endl;
  

return 0;
}

OUTPUT:

SCREENSHOT OF THE CODE:


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...
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...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT