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.
#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;
}
Comments
Thank you so much !!
Leave a comment