Removing extra blanks in array

The question says

The question with sample of the results after removing the blanks: http://i46.tinypic.com/wb2sn9.png

My solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int removeExtraBlanks(string &Sentence, int &length)
{
	int BlanksRemoved=0;
	if(length==0)
	{
		return -1;
	}
	else
		for(int index=0; index<length; index++)
		{
			if(Sentence[index]==' '  && Sentence[index+1]==' ')
			{
					Sentence[index+1]=Sentence[index+2];
					BlanksRemoved++;
					length--;
			}
		}
			return BlanksRemoved;
}


Can anybody tell me what's my mistake and what should I do?
Try checking the string::empty() function, rather than using ' '.
I hope this page would help a lot: http://cplusplus.com/reference/string/string/

The homework looks nice mann.

Thanks,
Aceix.
That was a fun little exercise. Try using a second string as an intermediary to store your results, and setting Sentence equal to that new string before you return. Ask if you need more help.
First of all according to the assignment you should use a character array. Character array is not std::string class.
As for your algorithm then it is invalid. To understand this try it when the character array consists from four characters that are all spaces.

If you are allowed to use standard algorithms then your assignment is done in one line by means of std::unique.
Last edited on
Careful! Testing for a ' ' char is not the same as a test for an empty string ("")

The solutions are pretty simple (once you see them!), but I think you should try and work it out for yourself. But I will say that the one that occurs to me first needs a second index variable in addition to the one on the loop (but not a second loop).

As it stands, your code is missing some of the blanks which need removing as it only looks for a single pair of blanks before stepping the loop index.

Also, the way you're using the length is suspicious.

Also
- string has a length() method, so there might be no need for the length param. as vlad drew our attention to, the first param should be a char[]
- why are you returning -1 for the length == 0 case? The return is the number of blanks removed.
Last edited on
Also in my opinion the function does not need the second parameter because the length of a character array thet represents a string is simply determined by finding the nul character.
Moreover the new length of the array is calculated as difference between the old length of the array and the number of removed spaces.
Even if the array does obligatory contain the nul character in any case you can calculate the new length as mentioned above/ So there is no need to declare the second parameter as a reference,

The code is simple if to use pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
int removeExtraBlanks( char *Sentence, int length )
{
	char *end = s + length;
	char *p = s, *q = s;

	while ( p < end ) 
	{
		*q = *p++;
		if ( *q++ == ' ' ) while ( p < end && *p == ' ' ) ++p;
	}

	return ( p - q );
} 


As you can see I removed the reference of the second parameter. The new length can be calculated as difference between the original length and the return value of the function.

Also I assumed that the character array does not contain the nul character.
Sorry, didn't notice that the array must be of type char, well.. now I changed from string to char and edited few things but it became complicated for me.

For andywestken, I used return -1, so when the array is empty, I check the return value in the main function if it's -1 I display an output message telling the user that the array is empty.

For vlad, sorry but I couldn't understand the code you wrote because I am mostly still studying the very basic things of C++, it is like an introduction to it and I didn't learn the purpose of * yet. In addition, I must follow the question details or requirements because my solution will be graded.
Last edited on
Well, the code

1
2
3
4
5
6
7
8
9
10
11
12
13
int removeExtraBlanks( char *Sentence, int length )
{
	char *end = s + length;
	char *p = s, *q = s;

	while ( p < end ) 
	{
		*q = *p++;
		if ( *q++ == ' ' ) while ( p < end && *p == ' ' ) ++p;
	}

	return ( p - q );
} 


can be simply rewritten without using pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int removeExtraBlanks( char Sentence[], int &length )
{
	int i = 0, j = 0;

	while ( i < length ) 
	{
		Sentence[j] = Sentense[i++];
		if ( Sentence[j++] == ' ' ) while ( i < length && Sentense[i] == ' ' ) i++;
	}

	length = j;

	return ( i - j );
} 


I did not test it but hope it will work.
Topic archived. No new replies allowed.