please help remove blanks from string

This is a discussion on please help remove blanks from string within the C Programming forums, part of the General Programming Boards category; This code is supposed to strip blanks in an array if any exist to the right of a non-blank, but ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    please help remove blanks from string

    This code is supposed to strip blanks in an array if any exist to the right of a non-blank, but leave the rest of the string as-is. Pretend input may or may not have blanks to the right and you want to lop them off if there, and leave the remaining string. For instance, if you enter, "abc 123 " the program *should print, "abc 123". (Notice the strlen of the first string is 10, and the second is 7, and the spaces are removed.)

    My problem is how to get the blanks out! I've tried to add a null terminator into the blank spot, and then test if a null terminator or valid character exists. If either of those are true, the for loop should exit, leaving any blank spaces to the right of a non-blank position...

    But it doesn't work. It just returns the whole string. I'm thinking this line, for(k=len; k=0; k -= 1) is causing the problem... I'll try this, for(k=len; line[k] != '\0'; k -= 1) ... but I just came up with that on the subway... so it hasn't been debugged.

    Also, I'm wondering if there's a better way of removing a blank than adding a '\0'.... hurmph...

    // strip.c
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>

    int main(void)
    {
    int k, len;
    char line[100];
    char *pt;

    pt = line;

    gets(line);
    len = strlen(line); // figure out length of string
    for(k=len; k=0; k -= 1) // for loop starts at right of array
    {
    if(isspace(*(line + k)) != 0 )
    *(line + k)='\0'; // if blank add null terminator
    else
    k=0;
    //if( (*(line + k)) == '\0' || isalnum(*(line + k) != 0 )) k = -1; // if '\0' or alnum exit
    }
    printf("%s %d", line, strlen(line));

    return 0;
    }
    Last edited by cjtotheg; 10-22-2001 at 09:46 AM.

  2. #2
    Unregistered
    Guest
    I think if you change

    for(k=len; k=0; k -= 1)

    to

    for(k=len, k>=0; k--)

    it will work.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    I finally got this to work by changing it this way:

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

    int main(void)
    {
    int k, len;
    char line[100];

    gets(line);
    len = strlen(line); // figure out length of string
    for(k=len; k>=0; k--) // for loop starts at right of array
    {
    if(isspace(line[k]) != 0) // isspace true (non-zero) when space exists
    line[k]='\0'; // if blank add null terminator
    if(isspace(line[k-1]) == 0) // if blank, break from loop
    break;
    else
    continue; // if non-blank, continue loop
    }
    printf("%s %d", line, strlen(line));

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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