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
#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;
}
Comments
Leave a comment