Answer to Question #46611 in C++ for amanda shaffer

Question #46611
Write a program that prompts the user to enter string and them prompts the user to enter a character. The program should display the location of the first occurrence of the character d, the program should say that the character is character number 5 in the string. If the character does not occur in the string, the program should display an appropriate message.
1
Expert's answer
2014-09-29T01:33:10-0400
#include <iostream>
#include <string>
using namespace std;
int main()
{
string st;
char ch;
cout << "Enter your string : ";
int pos = -1;
cin >> st;
cout<<"Enter your character : ";
cin >> ch;
bool here = false;
for (int i = 0; i < 100; i++)
{
if (st[i] == ch)
{
pos = i;
break;
}
}
if (pos == -1)
{
cout << "Character is not found";
}
else
{
cout << "character found on position " << pos + 1;
}
cin.get();
cin.get();
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