Deleting spaces in a string

This is a discussion on Deleting spaces in a string within the C Programming forums, part of the General Programming Boards category; Hi, I want to write a code which takes an array of 20 characters and deletes all spaces between characters, ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Unhappy Deleting spaces in a string

    Hi,
    I want to write a code which takes an array of 20 characters and deletes all spaces between characters, it should leave only letters. For example:
    input :a b c d e
    I want to make it : abcde

    I wrote this code, but it didn't change anything:


    Code:
    #include <stdio.h>
    
     int main ()
     {
             int i;
             char a[21];
    
    
    
             gets(a);
    
             for(i = 0; i < 20; i++)
             {
                     if(a[i] == 8 || a[i] == 9)
                     {a[i]= a[i+1];
                     i--;}
                     else continue;
             }
    
             printf("%s", a);
             printf("\n");
    
             return 0;
     }

    Can you help me with that? Thanks...

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    your searching if a[i]==8 or 9 whish is back space or horiztal tab then you making i+1 to that a and minunsing i after that ii dunno why your doing that anyways ?
    why dont you try copy anything inside the string thats not space
    if(a[i]!=' '){continue your function here}?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    if(a[i] == 8 || a[i] == 9) /* what is this statement supposed to do??? */

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    to catch if the character is a space or not (backspace and horizontal tab)

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ' ' == 32

    The other complication is that "i--" is not sufficient to compress the string.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Then use the character constant instead of its decimal value which depends on the underlying character set.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Don't do this
    Salem is being mean! There is nothing wrong with the OP. The one and only reply on Daniweb starts out with a completely moronic and incorrect analysis:
    You can't compile this code, that's why it didn't change anything: the else alternative has no correspondent if! Didn't you see it?
    What? I see an if and an else. I'd go somewhere "else" and ask again too.

    Chill out bahada.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    I solved this problem in another way thanks, and I asked at first at daniweb but i couldn't get the answer for hours, then I asked here..

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    function for deleting any character
    Attached Files Attached Files

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > but i couldn't get the answer for hours, then I asked here
    Oh, but there are answers there.
    So now you're claiming it's urgent
    Don't go round spamming multiple forums in the mistaken belief that you're entitled to a 24/7 service wherever you go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Don't go round spamming multiple forums in the mistaken belief that you're entitled to a 24/7 service wherever you go.
    That is not spamming, and you are not obliged to answer questions...I think.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 1
    Last Post: 03-08-2005, 11:02 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21