How to remove spaces from a string in C?

Asked

Viewed 16,290 times

5

I want to remove the spaces of a string, example:

Pedro Henrique Ribeiro

Would be:

Pedrohenriqueribeiro

The code I made, it takes away the space, but it 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;
}
  • Instead of changing the string you already have, generate a new one by adding a character to the character. When it’s space you skip the insertion ;)

  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site (if you have enough score).

4 answers

7

It would be easier to play in a string auxiliary, but if you are to do this, you need to separately control the reading progress of each character and the character that must be "copied". So every time you find a space, it needs to be ignored, then the space must increment the counter of the character that must be read definitively. The way done, it increases only at the time of picking, but the other characters will not feel the increment and return to pick 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);
}

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

  • Exclente! Thank you so much for your help

  • @Pribeiro Take a look at [tour]. You can accept an answer to your question and vote for anything on the website.

3

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

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

Follows 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 */

1

A very simple solution would be the code below, using an auxiliary string and the stdio library. h, however 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]=='\0')break;
    if(str[i]==' ')continue;
    str2[k]=str[i];
    k++;
}

printf("%s",str2);

return 0;

}

0

You can use a function rmv, that receives the String str and makes the modifications and makes the modifications in the variable itself.

#include<stdio.h>
void rmv(char *str){ 
    int count=0,i;
    for(i=0;str[i];i++){
        if(str[i]!=' '){
            str[count++]=str[i];
        }
    }
    str[count]=0;
} 
int main(){ 
    char str[]="Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
    rmv(str);
    puts(str);
    return 0;
}

Browser other questions tagged

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