63 203
Assignments Done
99,3%
Successfully Done
In July 2018

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.
Expert's answer
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions