Hai,im trying to do program using strings.The difficulty is that, i want to remove double spacing in the sentence.I dont know which function to use and the coding for it?please help me?

//to remove double spacing-task 6
int i,k=0;
for(i=0;i<s.length();i++)
{
if (s==' ')
{
????????????
????????????

Recommended Answers

All 8 Replies

Hi İ have trıed to remove the double spaces in the gıven string...but ıt doesnot work ıf u have more than 2 spaces in the sentesne...iam trying for that now(if we have more than 2 spaces in the given sentense..)

//removınf double spaces ın the gıven sentense...
#include<fstream>
#include<iostream>
#include<string>

using namespace std;

int main()	// returns 1 on error
{
	
	string s;

	cout<<"Enter the string...ın which u want to remove double spaces.."<<endl;
	getline(cin,s);
	//cout<<s<<endl;
	for(int j=0; j<s.length(); j++)
	{
		if(s[j]==' ' && s[j+1]==' ')
			s.erase(j,1);	
	}
	cout<<s<<endl;
	
	return 0;
}
Member Avatar for iamthwee

How about:

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
   string line;
    
   cout << "Enter a sentence:\n";
   
   getline(cin,line,'\n');
   

   stringstream ss(line);

   string word;
   string newLine = "";
   
   while ( ss >> word )
   {
     newLine = newLine + word + " ";
   }

   cout << "\n" <<newLine;
   cin.get();
   return 0;
}

Obviously it wipes out newlines and tabs which might be a problem.

Member Avatar for iamthwee

Perhaps a more robust solution which takes into account newlines and tabs might be...

#include <string>
#include <iostream>

using namespace std;

class Foo
{
public:
    void removeDoubleSpaces( string t ) 
    {  
      int i;
      i = t.find("  ");
      string ch;
      if ( i > -1 )
      {
        ch = t.replace( i,2," " );
        removeDoubleSpaces (ch); 
      }
      else if ( i == -1 )
      {
        cout << t << endl;    
      }  
    }
};      
    
int main()
{
   Foo bar;
   
   bar.removeDoubleSpaces("Oi ugly       \t why   you   \n looking \nfag?");
   cin.get();
   return 0;
}

Thnx.This works well but what if there were multiple spaces?????????

Member Avatar for iamthwee

Post number #4 works for multiple spaces as well, try it.

ya it works but the code seems to be confusing me but the code given by brk25 looks easy and i can understand.But it doesnt work for multiple spaces..

Member Avatar for iamthwee

That's because brk235 is wrong, but I am right.

It is very easy to understand. It just recursively replaces double spaces with single spaces until it no longer finds any double spaces in the string, thus keeping the tabs and newlines intact.

Maybe the class part is confusing you. So I recommend changing it to a simple function.

Please mark this as solved as well.

soryy, please delet this??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.