Answer to Question #40124 in C++ for Mary

Question #40124
Write a program that prompts the user to enter a string and then prompts the
user to enter a character.The program should display the number of times the
character occurs in the string.
1
Expert's answer
2014-03-24T11:16:55-0400
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int main()
{
string str;
char ch;
int amount = 0;
cout<<"Enter the string : ";
getline(cin, str);
cout<<"Enter the character to find : ";
cin>>ch;
for (int i = 0; i < str.size(); i++)
if(str[i] == ch) amount++;
cout<<"This character occurs in this string "<< amount <<" time(s)"<<endl;

_getch();
return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS