How to eliminate excess space in a string?

2

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

#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] = '
#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] = '%pre%';

}
int main()
{
   char frase[] = "Ola   Mundo!";
   removerSpacos(frase);
   printf("%s", frase);
   return 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.

    
asked by anonymous 26.03.2016 / 18:05

3 answers

2

Basically, the problem is not initializing the j variable. But the logic takes all the space, had to change a little with the JJao's warning in the comments I did 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] = '
#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] = '%pre%';
}
int main() {
   char frase[] = "Ola Mundo!";
   removerSpacos(frase);
   printf("%s", frase);
   return 0;
}
'; } int main() { char frase[] = "Ola Mundo!"; removerSpacos(frase); printf("%s", frase); return 0; }

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
26.03.2016 / 18:30
1

Contributing to this theme I made a blend of C / C ++, aiming to remove excess spaces, both at the end and middle of a string. See 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] = '
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] = '%pre%';
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] = '%pre%';
output.clear();
output.append(out);

return(output);
}
'; 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] = '%pre%'; output.clear(); output.append(out); return(output); }
    
08.03.2017 / 04:12
1

Alternative using regular Perl 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 : link
The pcrecpp.h file link

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.....
    
08.03.2017 / 08:33