Answer to Question #295012 in C++ for balla

Question #295012

how to remove unwanted space out of your answer?

i did a coding just like the one above but they are saying i have unwanted space in my output.


1
Expert's answer
2022-02-08T02:42:57-0500
#include<iostream>
#include<string>

using namespace std;

int main()
{
	string line="      London is one of the most popular\t cities in the world.\n\n"
	 			"	It is\t\t home to charming pubs, world-class\t (and often free)\n\n"
			    "   museums,      tons of history.";
	cout<<"Primary text:\n"<<line;
	
	string result="";
	for(int i=0;i<line.size();i++)
	{
		if(i==0)
		{
			while(line[i]==' '||line[i]=='\t'||line[i]=='\n')
				i++;
		}
		if(line[i]=='\t')
		{
			while(line[i]=='\t')i++;
		}		
		result+=line[i];
		if(line[i]==' ')
		{
			while(line[i]==' ')i++;
			i--;
		}
		if(line[i]=='\n')
		{
			while(line[i]=='\n')i++;
			if(line[i]==' ')
			{
				while(line[i]==' ')i++;
				i--;
			}
		}	
	}	
	cout<<"\nResulting text:\n"<<result;				
}

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