Search code examples
c++cstringcharacter-arrays

Removing spaces from a char array/cstring?


I cannot get my program to delete extra spaces that are found within the sentence, like if there is another space after a space. And the while loop I'm using to ask the user to continue is preventing the functions from running. When I remove it, the rest of the program runs.

This is the main function:

int main()
{
    bool program_start = 1;
    char user_sentence[MAX];

    cout<<"This program will take any sentence you write and correct any spacing\n"
        <<" or capitalization (not proper nouns) mistakes you have.\n";

    while(loop_continue(program_start))
    {
        input(user_sentence, MAX);
        uppercase_spacing(user_sentence);
        lowercase(user_sentence);
        cout<<user_sentence;
    }

    return 0;
}

My problem is with my uppercase_spacing function, I cannot get it to delete the extra spaces that are within the sentence.

Here are the void functions I'm using to edit the sentences:

void uppercase_spacing(char sentence[])
{
    int number = 0;
    if(isspace(sentence[0]))
    {
        for(int i = 0; i < MAX; i++)
        {
            sentence[i] = sentence[i + 1];
        }

        while(number<MAX)
        {
            if((isspace(sentence[number])) && (isspace(sentence[number+1])))
            {
                sentence[number] = sentence[number + 1];
            }
            number++;
        }

    sentence[0] = toupper(sentence[0]);

    }else{

        for(int index = 0; index<MAX;index++)
        {
            if((isspace(sentence[index])) && (isspace(sentence[index+1])))
                sentence[index]=sentence[index+1];
        }

        sentence[0] = toupper(sentence[0]);

    }
    return;
}

void lowercase(char sentence[])
{
    for(int i = 1; (i < MAX) && (i != '\0'); i++)
    {
        sentence[i] = tolower(sentence[i]);
    }

    return;
}

I use this boolean in all of my other programs, so I believe the problem is within the main part of the program.

Here is the boolean function I'm using for my while loop:

bool loop_continue(bool another_round)
{
    bool again = another_round;
    char continue_loop = 'y';
    cout<<"Would you like to continue? Type y or Y for yes,\n" 
        >>" or any other letter for no.\n";
    cin>> continue_loop;
    if((continue_loop == 'y' )||(continue_loop == 'Y'))
    {
        cout<<"Okay, let's do this!"<<endl;
        again = 1;
    }else
    {
        cout<<"Goodbye."<<endl;
        again = 0;
    }
    return again;
}

The input; the quotes are to stand in for the spaces.:

Candy CCandy " " " "something

The output; the spaces are still there:

Candy ccandy " " " "something


Solution

  • First, there are a number of off-by-one errors in your code. (The maximum index in char sentence[MAX] is MAX-1, and you have a number of cases where the loop body runs with index=MAX-1 and you access sentence[index+1]).

    Second, let's dig into what your function is doing...

    for(int index = 0; index<MAX;index++)
    {
        // if sentence[index] and sentence[index+1] are both spaces....
        if((isspace(sentence[index])) && (isspace(sentence[index+1])))
            // set sentence[index] to sentence[index+1] (??!)
            sentence[index]=sentence[index+1];
    
        // now proceed to the next character
    }
    

    Do you see the problem now? You are setting a character known to be a space (sentence[index]), to a character known to be a space (sentence[index+1]). You are not actually removing any spaces.