Hey guys I'm having a strange problem with my program and I'm not sure how to solve the problem.

I have a function which takes an array clears multiple white spaces to make a sentence so

"abc    def       ghi"
//becomes
"abc def ghi"

and it returns the length of the array. It is supposed to be 39 characters but my array keeps sending back 40 characters, if you could look into the program and help me out please.

#include<iostream>

using namespace std;
int cleanSpace(char s[]);

int main()
{
	char d[] =    "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx";
	
       int x = strlen(d);

	strcpy(d, "  widget;Acme  \t  \n Co.;gear    induction\tdevice:\v   \n ");

	int result = cleanSpace(d);
	if(result != 39)
	{
		printf("Failed test 7: cleanSpace should have returned 39, "
				"your function returned %d\n\n", result);
		exit(7);
	}

	cout << "\n" << result << "\n";

	return 0;
}

int cleanSpace(char s[])
{
	char temp[251];
	int temp_index = 0;

	int x = strlen(temp);
        
        //create NULL array
	for(int i = 0; i != x; i++){
		temp[i] = '\0';
	}

       //remove certain special characters
	for(int i = 0; s[i]; i++){
		if(s[i] == '\t' || s[i] == '\n'){
			s[i] = ' ';
		}
	}

	//Remove leading spaces.
	int i = 0;
	while(s[i] == ' '){
		i++;
	}

	//Remove middle spaces.
	for(i; s[i]; i++){
		if(s[i] != ' '){
			temp[temp_index++] = s[i];
		}
		else{
			if(s[i] == ' ' && s[i + 1] != ' '){
				temp[temp_index++] = s[i];
			}
		}
	}

	temp[temp_index] = '\0';
	
	cout << temp << "\n";
	strcpy(s, temp);
	return strlen(s);
}

line 42: since the array temp was not initialized, calling strlen() will produce unpredictable and unreliable results. You don't want strlen() of temp but sizeof(temp) so that it can be used in lines 45 and 46.

You can replace lines 42-47 with just one line char temp[251] = {0};

You can replace lines 42-47 with just one line char temp[251] = {0};

Tried what you said but getting the same result. :-/

Tried what you said but getting the same result. :-/

Perhaps your intention was to replace also a '\v' with a space but you forgot to do it?

I proudly declare this thread solved.

Thanks guys for all the help. I managed to figure out the problem, my if statement was a little buggy kept adding an space at the end of the array:$ I should have checked the statement properly, I need to debug better.

Did you replace all the necessary lines with the one initializer ? That makes it work for me.

This might be a cleaner alternative if you want to use strtok to do your work to split the string up

int myCleanSpace(char s[]){

        char* ptr;
        char delims[] = {" \n\t"}; //break the string into tokens 
                                             // when you read a space, a tab
                                             // or a newline. 
        string final; 

        ptr = strtok(s,delims);

        while (ptr != NULL){
          final += ptr;  
          final += " ";
          ptr = strtok(NULL,delims);
        }

        strcpy(s,final.c_str());
        return strlen(s); 
      }
This question has already been answered. Start a new discussion instead.