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
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...
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...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT