954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

removing spaces from string

i need help removing spaces from a string..how do i go about this?? i have to report back the number of spaces too but ive been able to do that

thnx

(i dont want to use any of the string functions in the string library)

#include <iostream>

int stripspaces(char *str);

using namespace std;

int main()
{
	char ar[100];

	cout<<"Enter a sentence and I will strip out the spaces:"<<endl;

	cin.getline(ar, 99);

	cout<<"Your sentence without spaces is "<<stripspaces(ar)<<endl;



	return 0;
}

int stripspaces(char *str)
{

	int spacecounter = 0;

	char *p= str;

	while(*p)
	{
		if(*p == ' ')
		{	

                                     //dont know how to set this up for removing spaces

			spacecounter++;	
		}


		p++;	
	}



	return spacecounter;

}
unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

See recent thread that dealt with similar issue.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

i tried looking at your solution but dint really understand..could u plz explain

thnx

int main() {
   uint64 cycle_timer_beg;
   uint64 cycle_timer_end;

   cycle_timer_beg = GetCycleCount();

   for (int j = 0; j < 10000; ++j) {

      char arr[] = "abc     def";
      char temp[sizeof arr];
      int temp_index = 0;
      for (int i = 0; arr[i]; ++i) {
         if (arr[i] != ' ') {
            temp[temp_index++] = arr[i];
         }
      }
      temp[temp_index] = '\0';
      strcpy_s(arr, sizeof(arr), temp);

   }

   cycle_timer_end = GetCycleCount();

   cout << "Average cycle count: " << ((cycle_timer_end - cycle_timer_beg) / 10000);
   cin.ignore();
   return 0;
}
unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

ok..i think i have an idea but im having a problem with the bold part..i know i cant do that but how can i correct it??

#include <iostream>

int stripspaces(char *str);

void printwithoutspace(char *str);

using namespace std;

int main()
{
	char ar[100];
	
	cout<<"Enter a sentence and I will strip out the spaces:"<<endl;
	
	cin.getline(ar, 99);

	return 0;
}

int stripspaces(char *str)
{

	int spacecounter = 0;
	
	char *p= str;

	while(*p)
	{
		if(*p == ' ')
		{		
			spacecounter++;	
		}


		p++;	
	}

	<strong>char ar[] = str;</strong>
	for (int i = 0; ar[i]; i++) 
	{
         if (ar[i] != ' ') 
		 {
            char strA[99];

			strcpy(strA, str);

			cout<<"Your sentence without spaces is "<<strA<<endl;

         }
    }

	return spacecounter;

}
unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

ok i fixed that but now ive got another issue my loop..cant seem to locate the problem...plz help

#include

int stripspaces(char *str, char ar2[]);

using namespace std;

int main()
{
	char ar[100];
	
	cout<<"Enter a sentence and I will strip out the spaces:"<<endl;
	
	cin.getline(ar, 99);

	cout<<"Your sentence has "<<stripspaces(ar, ar)<<" spaces"<<endl;


	return 0;
}

int stripspaces(char *str, char ar2[])
{

	int spacecounter = 0;
	
	char *p= str;

	while(*p)
	{
		if(*p == ' ')
		{		
			spacecounter++;	
		}


		p++;	
	}


	<strong>for (int i = 0; ar2[i] != ' '; i++) 
	{
            char strA[99];

			strcpy_s(strA, str);

			cout<<"Your sentence without spaces is "<<strA<<endl;

         
    }</strong>
	return spacecounter;
}
unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
for (int i = 0; ar2[i] != ' '; i++) 
	{
            char strA[99];
	     strcpy_s(strA, str);
             cout<<"Your sentence without spaces is "<<strA<<endl;         
        }


Line 1 - That loop condition won't work. It stops your loop at the first blank.
Line 3 - Array declaration needs to come BEFORE the loop starts. You just keep creating and deleting the array if it's inside the loop.
Line 4 - What exactly are you trying to do with strcpy?
Line 5 - cout statement should come AFTER the loop when the loop is all done.

Note that you don't use the variable i at all in the loop here. I also see THREE arrays here (str, strA, and ar2). Do you want/need all three of them? If so, why? Two is plenty and it could even be done with one if you wanted to. But stick with two. It's probably easier. But I can't think of any reason you'd want three. Let's say you decide to keep str and ar2.

You need to think about exactly what you want str and ar2 to contain after this loop is done. What do each contain BEFORE the loop is started. You need to be very clear on this before designing the loop. Keep in mind that right now, since you passed the function the same argument, they are actually pointing to the same thing.

Also ask yourself whether you really want this function to take two arguments, and if so, what do they represent?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

well i tried doing it with 1 argument but didnt know how to set something in my function equal to the string..apart from the pointer.

or lets just say this how can i do this with 1 argument?

unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

well i tried doing it with 1 argument but didnt know how to set something in my function equal to the string..apart from the pointer.

or lets just say this how can i do this with 1 argument?

int stripspaces(char *str)
// str contains full string when it is passed to the function.
// str contains string without whitespace when function is done.


It can be done both with or without creating a temporary array. It's probably easier using the second array, so create a second char array called temp and copy str into it. Go through temp one character at a time. Keep track of how many non-blank characters you run into in temp. Start at the front of the str array (ie. str[0]). When you hit a non-blank, copy that character to str and increment the non-blank characters counter, which keeps track of what index of str to copy the next non-blank-character of temp to. When you've gone all the way though temp, add a null terminator to str.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

#include

int stripspaces(char *);

using namespace std;

int main()
{
char ar[100];

cout<<"Enter a sentence and I will strip out the spaces: ";
cin.getline(ar,100);

cout<<"No of spaces: "<

Rhohitman
Junior Poster in Training
86 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
 

this here is broken but i think i am just mishandling the pointers. but the overall logic should get done what u need.

#include

void stripspaces(char *, char *);

using namespace std;

int main()
{
char ar[100];
char *stringwithoutspaces=0;

cout<<"Enter a sentence and I will strip out the spaces:"<

absentshadow
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

i dont know what happened to my tabs

absentshadow
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

This above code was the some thing like using the pointer arithmetics

:)

Rhohitman
Junior Poster in Training
86 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
 

But this is the C++ forum after all, so I would suggest a C++ solution rather then a C solution.

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

using namespace std;

void StripSpaces(string);

int main()
{
    string str = "hello world with spaces";
    StripSpaces(str);
    return 0;
}

void StripSpaces(string strin)
{
    stringstream in(strin);
    stringstream out;
    string temp;
    while (in >> temp)
        out << temp;
    cout << out.str();
}


The >> operator in stringstream disregards any whitespace for you, so I took advantage of this property.
You see how clean the code looks? Hooray for c++! :)

Ps. for everone that didn't use code-tags in their reply : CLICK ME

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

ok no offence to others but i think im gona with vernon's for now..but thnx for the help.

im still havin problems though and hopin some1 can help me out with his way. heres what i have so far

im having a problem doing the copying part

#include <iostream>

int stripspaces(char *str, char ar2[]);

using namespace std;

int main()
{
	char ar[100];
	
	cout<<"Enter a sentence and I will strip out the spaces:"<<endl;
	
	cin.getline(ar, 99);

	cout<<"Your sentence has "<<stripspaces(ar, ar)<<" spaces"<<endl;


	return 0;
}

int stripspaces(char *str, char temp[])
{

	int spacecounter = 0;
	
	char *p= str;

	while(*p)
	{
		if(*p == ' ')
		{		
			spacecounter++;	
		}

		p++;	
	}

	return spacecounter;

	int i = 0;

	while(temp[i])
	{
		int count;

		count = 0;
<strong>
		if(temp[i] != ' ')
		{

	//stuck here

			count++;
		}

		i++;
	}</strong>
	cout<<"Your string without spaces is"<<str<<endl;

}
unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
ok no offence to others but i think im gona with vernon's for now..

Non taken. But Iam curious why you choose for a C solution in a C++ program, please clarify


[edit]
im having a problem doing the copying part

What's the problem?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

well as u must have noticed by now its my first time programming and im only gona need this for about another week to get through this class, till sometime from now when i may come bak to it

unfortunately as i mentioned in my original post i dont want to use any of the string library functions since i supposedly dont know those because we havent been taught them

if possible, kindly guide me to vernons approach

i pretty much dont know how to set that part up..how does it look so far though??

unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
unfortunately as i mentioned in my original post i dont want to use any of the string library functions since i supposedly dont know those because we havent been taught them


'aight, I missed that part in your original post, sorry about that.

You still didn't tell what problems you have with your own code, but one thing I see straight away is this: return spacecounter;

This breaks out of the function and the code below it will never be executed, so that might be a bit of a problem for you ;)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

ill just put that at the bottom then..yeah the problem i dont know how to set it up to do the copying..i was able to implement part of his solution

int stripspaces(char *str, char temp[])
{

	int spacecounter = 0;
	
	char *p= str;

	while(*p)
	{
		if(*p == ' ')
		{		
			spacecounter++;	
		}

		p++;	
	}


	int i = 0;

	while(temp[i])
	{
		int count;

		count = 0;

		if(temp[i] != ' ')
		{

	

			count++;
		}

		i++;
	}

	cout<<"Your string without spaces is"<<str<<endl;


	return spacecounter;

}
unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

You'll have to add something like:

str[i] = temp[count];
inside your if-statement to copy the array without whitespaces.

Out of interest:

while(*p)
	{
		if(*p == ' ')
		{		
			spacecounter++;	
		}

		p++;	
	}

What's this suppose to do? Is it a requirement that you count all whitespaces? If not: delete this

[edit]
Ok it IS a requirement. I should learn to read OP better :)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

yeah it is

unk45
Junior Poster in Training
65 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You