Answer to Question #158074 in C++ for Ahtisham Arshad

Question #158074

Write a C++ program in which, read a c-string from user. Now your task is to replace all possible pairs of  character from input string. 

For example:

Input: 

Array: good 

Output: 

Array: gd 



1
Expert's answer
2021-01-25T00:53:30-0500
#include <iostream>
#include <string>

using namespace std;

int main()
{
	cout << "Enter a string: ";
	string s1;
	string s2;
	getline(cin, s1);
	while(true)
	{
		for(int i = 0; i < s1.length(); i++)
		{
			if(i == s1.length() - 1) // check last character in string 
			{
				s2 += s1[i];
				break;
			}
			if(s1[i] == s1[i+1]) // check are current and next character equals
			{
				i++;
				continue;
			}
			s2 += s1[i];
		}
		if(s1 == s2) break;
		s1 = s2;
		s2.erase();
	}
	cout << "Output string is: " << s1 << endl << endl;
	cout << "Enter any char and press 'Enter' to close program: ";
	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