Answer to Question #229966 in C++ for Sully

Question #229966

Write a program to enter a multiline string, by taking input from the user. Remove all the multiple blank spaces by copying the string to another string. Deallocate the memory for first string. Display the second string.


1
Expert's answer
2021-08-27T02:03:46-0400
#include <iostream>
#include <string>
using namespace std;


void removeMultipleBlankSpaces(string text)
{
	int length=text.length();
	int i=0,j=0;
	for(i=0; i<length; i++)
    {
        if(text[0]==' ')
        {
            for(i=0; i<(length-1); i++)
                text[i] = text[i+1];
            text[i] = '\0';
            length--;
            i=-1;
            continue;
        }
        if(text[i]==' ' && text[i+1]==' ')
        {
            for(j=i; j<(length-1); j++)
            {
                text[j] = text[j+1];
            }
            text[j] = '\0';
            length--;
            i--;
        }
    }
	cout<<"\nText without spaces:\n";
	cout<<text<<"\n";
}
 


int main(){
	string* text=new string();
	cout<<"Enter text: ";
	string line;


	while (true) {
		getline(cin, line);
		if (line.empty()) {
			break;
		}
		text->append('\n' + line);
	}
	cout<<"\nCurrent text:\n";
	cout<<text->c_str()<<"\n";
	removeMultipleBlankSpaces(*text);
	
	delete text;
	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

Sully
30.08.21, 08:36

Thank you so much !!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS