How to eliminate excess spaces in a string?

Asked

Viewed 3,377 times

2

C code to remove strings spaces is not working. It stops at execution.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void removerSpacos(char str[]) {
int i, j;
int tam = strlen(str);
for(i=0; i<tam; i++) {
        if(str[i] != ' ') {
                str[j] = str[i];
                j++;
    }
}
   str[j] = '\0';

}
int main()
{
   char frase[] = "Ola   Mundo!";
   removerSpacos(frase);
   printf("%s", frase);
   return 0;
}

How to leave the string with 1 space? Between the words, there in the phrase "Hello World" has 2, and in the code it removes all spaces.

3 answers

2


Basically the problem is not having initialized the variable j. But logic takes away all space, I had to change a little with the alert of Jjoao in the comments. I made an optimization, organized and modernized the code:

#include <stdio.h>

void removerSpacos(char str[]) {
    int j = 1;
    for (int i = 1; str[i]; i++) {
        if (str[i] != ' ' || (str[i - 1] != ' ')) {
           str[j] = str[i];
           j++;
        }
    }
    str[j] = '\0';
}
int main() {
   char frase[] = "Ola Mundo!";
   removerSpacos(frase);
   printf("%s", frase);
   return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    @Jjoao do not know what happened, I could swear I was right. I tidy up. I wanted to follow his algorithm, but it really doesn’t work if the intention is to leave some.

1

Contributing to this theme I mixed C/C++, aiming to remove the excess spaces, both at the end and in the middle of a string. Look at the code I got

string RemoveEspaco(string str)
{
int i, j;
char *input, *out;
string output;

input = new char[str.length()+1];
strcpy(input,str.c_str());

for (i=strlen(input); i > 0; i--)
    {
    if (input[i] != ' ')
        {
        break;
        }
    }
input[i] = '\0';
out = new char[strlen(input)+1];
for (i=0,j=0;i < strlen(input); i++)
    {
    if (i > 1)
        {
        if (input[i] != ' ')
            {
            out[j] = input[i];
            j++;
            }
        else
            {
            if ((input[i] == ' ')&&(input[i-1] != ' '))
                {
                out[j] = input[i];
                j++;
                }
            }
        }
    else
        {
        out[j] = input[i];
        j++;
        }
    }
out[j] = '\0';
output.clear();
output.append(out);

return(output);
}

1

Alternative using Perl regular expression

// inclui a Perl Compatible Regular Expression
#include <pcrecpp.h>


char str = "a       b     c";
str = pcrecpp::RE("!\s+!").Replace(" ", &str);
printf(str); // retorna "a b c"

About the Perl library: http://www.pcre.org/
The pcrecpp. h file https://github.com/vmg/pcre/blob/master/pcrecpp.h

Example of how you can implement in the question script:

#include <stdio.h>
#include <pcrecpp.h>

void removerSpacos(char str[]) {
    str = pcrecpp::RE("!\s+!").Replace(" ", &str);
}

// aqui o restante dos seus códigos
int main() {
   char frase[] = "Ol.....
  • @ Daniel, I liked it (+1) but it was difficult to compile it. I wonder if you could add as little as necessary to make the compilation possible?

  • @Jjoao, added example and link to pcrecpp. h

Browser other questions tagged

You are not signed in. Login or sign up in order to post.