Advertisement
obernardovieira

Remove space from char array (2 ways)

Apr 10th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     const char* frase = "oi pessoal da vida !";
  6.     char* dest = (char*) malloc (sizeof(frase)+1);
  7.    
  8.     int f=0,d=0;
  9.     while(frase[f++] != '\0') {
  10.         if(frase[f-1] != ' ')
  11.             dest[d++] = frase[f-1];
  12.     }
  13.    
  14.     print(dest);
  15.     return 0;
  16. }
  17.  
  18.  
  19. //or
  20. //not by me
  21.  
  22. #include <stdio.h>
  23.  
  24. void remove_space(char *d, const char *s){
  25.     for(;*s;++s){
  26.         if(*s != ' ')
  27.             *d++ = *s;
  28.     }
  29.     *d = *s;
  30. }
  31.  
  32. int main(){//DEMO
  33.     char secuencia[] = "1 2 3 4 5 6 7 8 9";
  34.     char temp[sizeof(secuencia)];
  35.     remove_space(temp, secuencia);
  36.     puts(temp);//123456789
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement