Question

Please do it with C++. Thanks:) Write a program that reads a string and a character...

Please do it with C++. Thanks:)

Write a program that reads a string and a character from the keyboard. The program should output how many times the character appears in the string. Make the program run in a loop until the string finish is input.

Sample run:

Input a string: alicia
Input a letter: a
The letter appears 2 times
Input a string: felix
Input a letter: x
The letter appears 1 time
Input a string: finish
Good Bye.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
int main() {
string str;
char ch;
int count = 0;
  
do{
count = 0;
cout<<"Input a string: ";
cin>>str;
if(str=="finish"){
cout<<"Good Bye.";
break;
}
cout<<"Input a letter: ";
cin>>ch;
for(int i=0 ;i< str.size();++i){
  
if(str.at(i)==ch)
++count;
  
}
cout<<"The letter appears "<<count<< " times"<<endl;
  
}while(1);
}



===================================================
See Output

Files saved share gcc version 4.6.3 Input a string alicia Input a letter: a The letter appears 2 times Input a string: fekix

Thanks, PLEASE RATE. let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Please do it with C++. Thanks:) Write a program that reads a string and a character...
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
  • Write a program whose input is a string which contains a character and a phrase, and...

    Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: t this is a sentence with many t's the output is: 4 PLEASE WRITE USING PYTHON

  • Write a program whose input is a string which contains a character and a phrase, and...

    Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...

  • Write a program in python that asks the user to input a sentence. The program will...

    Write a program in python that asks the user to input a sentence. The program will ask the user what two letters are to be counted. You must use a “for” loop to go through the sentence & count how many times the chosen letter appears in the sentence. You are not allowed to use python built-in function "count()" or you'll get a Zero! Output will show the sentence, the letter, and the number of times the letter appears in...

  • Write a program whose input is a character and a string, and whose output indicates the...

    Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday, the output is: 1 Ex: If the input is: z Today is Monday, the output is: 0 Ex: If the input is: n It's a sunny day, the output is: 2 Case matters. Ex: If the input is: n Nobody, the output is: 0 n is different than...

  • 4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n...

    4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n is the integer value in matrix B which is having the same index as that of the character taken in A). Solve this using 1D Block. Example: 1 2 4 3 e X a M Output String STR: pCCaaaaPPPeeXXXXaaaMM 4B. Write...

  • Java Letter Counter: Write a program that asks the user to enter a string, and then...

    Java Letter Counter: Write a program that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. Must use a do while loop and a counter!!!!

  • Create an algorithm for a program that reads a string with a maximum length of 30...

    Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm should then copy the characters from that string into a second character array in order but only if the character is a vowel (a, e, i, o, u). Once copied, the algorithm should output these values in order. The algorithm should then count and display the number of times each vowel appears in the array. Finally, the algorithm should...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • Python Write a program which reads in a string and an signifying the number times the...

    Python Write a program which reads in a string and an signifying the number times the string should be repeated. The program should then output the string N times. integer N, You can assume all input will be valid. Example: Enter a string: hello How many times to repeat? 5 hello hello hello hello hello Example: T Enter a string: goodbye Ty How many times to repeat? 32 goodbye goodbye [... goodbye 30 more times]

  • Write a Java program that reads in a word from the user and outputs each character...

    Write a Java program that reads in a word from the user and outputs each character of the word on a separate line. For example, if the user enters the input "Joe", your program should output: J o e You need to use a for-loop.

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