How to remove spaces from a string in C?

4

I want to remove spaces from a string , example:

Pedro Henrique Ribeiro

I would stay:

Pedrohenriqueribeiro

The code I made, removes the space, but duplicates a letter:

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

int main(int argc, char**argv){

    char string[101];
    int x, i, a;

    printf("Foneca uma string com espacos em branco: ");
    gets(string);
    printf("%s\n",string);

    for(i=0; i<strlen(string); i++){
        if(string[i]==' '){
            string[i]=string[i+1];
        }   
    }

    printf("String sem espaços em branco: %s\n", string);

    return 0;
}
    
asked by anonymous 06.05.2016 / 16:19

3 answers

7

It would be easier to play in an auxiliary string , but if you are to do this, you must separately control the reading progress of each character and the character to be "copied". So every time you find a space, it needs to be ignored, so the space must increment the counter of the character that should be read definitively. The way it is done, it increases only at the time of picking, but the other characters will not feel the increment and come back to get the wrong character.

#include <stdio.h>
#include <string.h>
int main() {
    char string[101];
    printf("Forneca uma string com espacos em branco: ");
    fgets(string, sizeof(string), stdin);//mudei aqui para modernizar. ideone ñ aceita gets
    int len = strlen(string); //só pra dar melhor performance
    printf("%s\n", string);
    for (int i = 0, posicao = 0; i < len; i++, posicao++) {
        if (string[posicao] == ' ') {
            posicao++;
        }
        string[i] = string[posicao];
    }
    printf("String sem espaços em branco: %s\n", string);
    return 0;
}

See running on ideone .

    
06.05.2016 / 16:45
3

Characters representing blank spaces can be tested using the isspace() function of the default library ctype.h .

The secret is to scan the input string, character to character, and if the character read is different from a blank space, it will be copied to the output string, otherwise it will be ignored.

Follow the source code:

#include <stdio.h>
#include <ctype.h>

char * remove_espacos( char * out, const char * in )
{
    const char * p = in;
    int i = 0;

    while( *p )
    {
        if( !isspace(*p) )
            out[i++] = *p;

        p++;
    }

    out[i] = 0;

    return out;
}


int main( int argc, char * argv[] )
{
    const char * entrada = "Um pequeno jabuti xereta viu dez cegonhas felizes.";
    char saida[ 100 ] = {0};

    remove_espacos( saida, entrada );

    printf("Entrada: %s\n", entrada );
    printf("Saida: %s\n", saida );

    return 0;
}

/* eof */

I hope I have helped!

    
06.05.2016 / 23:00
1

A very simple solution would be to the code below, using an auxiliary string and the stdio.h library, but it is not the most optimized.

int main()
{
    int i=0,k=0;
    char str[51],str2[51]="Ola";

fgets(str,51,stdin);

for(;i<51;i++){
    if(str[i]=='
int main()
{
    int i=0,k=0;
    char str[51],str2[51]="Ola";

fgets(str,51,stdin);

for(;i<51;i++){
    if(str[i]=='%pre%')break;
    if(str[i]==' ')continue;
    str2[k]=str[i];
    k++;
}

printf("%s",str2);

return 0;
')break; if(str[i]==' ')continue; str2[k]=str[i]; k++; } printf("%s",str2); return 0;

}

    
13.05.2018 / 15:33