Answer to Question #157973 in C++ for Babar wattoo

Question #157973

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


1
Expert's answer
2021-01-24T11:09:36-0500
#include <iostream>
#include <string>
#include <vector>
//*************************************************************************
using namespace std;
//*************************************************************************
int main()
{
	cout << "Enter a string (without spaces): ";
	string InitialString;
	getline(cin, InitialString);
	vector<char> WorkingVector;
	for(int i = 0; i < InitialString.length(); i++)
	{
		WorkingVector.push_back(InitialString[i]);
	}
	int NumberOfIterations = WorkingVector.size();
	for(int i = 0; i < NumberOfIterations; i++)
	{
		for(int j = 1; j < WorkingVector.size(); j++)
		{
			if(WorkingVector[j] == WorkingVector[j-1])
			{
				WorkingVector.erase(WorkingVector.begin() + j - 1);
				WorkingVector.erase(WorkingVector.begin() + j - 1);
				j--;
			}
		}
	}
	string FormatedString;
	for(int i = 0; i < WorkingVector.size(); i++)
	{
		FormatedString += WorkingVector[i];
	}


	cout << "Formated string is: " << FormatedString<< endl;
	cout << "Enter something and press 'Enter' to terminate the program: ";
	string msg;
	cin >> msg;
	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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS