Answer to Question #201692 in C++ for AJUYDGSN

Question #201692

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”.


1
Expert's answer
2021-06-01T11:33:03-0400
#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;
}

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
New on Blog