Write a program to accept a string and a character. The program should print the remaining part of the string after the first occurrence of the character in the string. If the character does not occur in the string then it should “Character ‘input character’ does not occur in the given string”.
#include <iostream>
#include <string>
using namespace std;
int main() {
string inputString;
char character;
int index=-1;//index of the first occurrence of the character in the string
//accept a string and a character.
cout<<"Enter a string: ";
getline(cin,inputString);
cout<<"Enter a character: ";
cin>>character;
for(int i=0;i<inputString.length();i++){
if(inputString[i]==character){
index=i;
break;
}
}
if(index!=-1){
//print the remaining part of the string after the first occurrence of the character in the string.
for(int i=index+1;i<inputString.length();i++){
cout<<inputString[i];
}
cout<<"\n\n";
}else{
//If the character does not occur in the string then it should “Character ‘input character’ does not occur in the given string”.
cout<<"\nCharacter '"<<character<<"' does not occur in the given string.\n\n";
}
system("pause");
return 0;
}
Comments
Leave a comment