Remove Signs & Whitespace (w/pointer)

This is a discussion on Remove Signs & Whitespace (w/pointer) within the C Programming forums, part of the General Programming Boards category; Hey! Could you by any chance give me some hints why the following function does not work the way it ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    38

    Remove Signs & Whitespace (w/pointer)

    Hey!

    Could you by any chance give me some hints why the following function does not work the way it should?

    Code:
    void removeSign(char *str)
    {
        char *ptr = str;
        while (*ptr != '\0') {
            while(!(*ptr > 64 && *ptr < 91) && !(*ptr > 96 && *ptr < 123))
            {
                ptr++;
            }
            *str = *ptr;
            str++;
            ptr++;
        }
    }
    all it should do is remove every sign which isn't a character (a-z, A-Z).

    Edit: it only works on some inputs...

    I'm especially confused because my similar "remove all whitespace" function seems to work just fine:

    Code:
    void removeWhitespace(char *str)
    {
        char *ptr = str;
    
        while (*ptr != '\0')
        {
            while (isspace(*ptr))
            {
                ptr++;
            }
            *str = *ptr;
            str++;
            ptr++;
        }
    }
    Last edited by coffee_cup; 11-09-2012 at 10:02 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > all it should do is remove every sign which isn't a character (a-z, A-Z).
    Perhaps replace the test in the code which works
    while ( isspace

    With say
    while ( !isalpha
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Add final
    *str = '\0';

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    check out the 'isalpha' function and others in ctype.h

    its because in the last iteration of the loop, if it is a non-alpha, you increment ptr. then you increment ptr again which skips over the '\0' and runs off the end of the string.
    i think you got lucky on the removeWhitespace function. it has the same problem.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Thanks alot for the suggestion, I did not know of "isalpha", handy

    It didn't do the trick though, the issue remains the same =/

    As said I also don't get why whitespace function would work like a charm while Alpha one doesn't for some reason hm

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <String.h>
    #define ZEICHEN 40
    
    int s_length, i, j = 0;
    char sentence[ZEICHEN];
    char *poscheck;
    
    void removeWhitespace(char *str)
    {
        char *ptr = str;
    
        while (*ptr != '\0')
        {
            while (isspace(*ptr))
            {
                ptr++;
            }
            *str = *ptr;
            str++;
            ptr++;
        }
    }
    
    void makeLower(char *str)
    {
        while (*str != '\0')
        {
            *str = tolower(*str);
            str++;
        }
    }
    
    void removeSign(char *str)
    {
        char *ptr = str;
        while (*ptr != '\0') {
            while (!isalpha(*ptr))
            {
                ptr++;
            }
            *str = *ptr;
            printf("%c", *ptr);
            str++;
            ptr++;
        }
    }
    
    int main()
    {
    
        while(1)
        {
            printf("palindrome\n");
            printf("Enter a message: ");
            fgets(sentence,ZEICHEN,stdin);
            removeWhitespace(sentence);
            makeLower(sentence);
            removeSign(sentence);
    
            s_length = strlen(sentence);
            poscheck = sentence;
    
            for (i = 0; i < s_length/2; i++)
            {
                if(!(*(sentence+i) == *(poscheck+s_length-1-i)) || (strchr(sentence, '\n')))
                {
                    printf ("Not a palindrome");
                    exit(1);
                }
            }
            printf ("Palindrome\n");
        }
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Quote Originally Posted by Shurik View Post
    Add final
    *str = '\0';
    Quote Originally Posted by dmh2000 View Post
    check out the 'isalpha' function and others in ctype.h

    its because in the last iteration of the loop, if it is a non-alpha, you increment ptr. then you increment ptr again which skips over the '\0' and runs off the end of the string.
    i think you got lucky on the removeWhitespace function. it has the same problem.
    Ah thanks will look into that brb ^^

  7. #7
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Add '\0' in the end of str and after that if again it's not working properly, provide the input and corresponding output.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    thanks once again!

    ok two changes:
    1) added && (*ptr != '\0') to inner while loop to prevent incrementation on \0
    2) added *str = '\0'; as proposed

    Code:
    void removeSign(char *str)
    {
        char *ptr = str;
        while (*ptr != '\0') {
            while (!isalpha(*ptr) && (*ptr != '\0'))
            {
                ptr++;
            }
            *str = *ptr;
            str++;
            ptr++;
        }
        *str = '\0';
    }
    it now seems to work. I however don't get what *str = '\0'; really does in this case. Could you enlighten me by any chance? Why is it needed?

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    At this point, 'str' holds the address of the end of the string (specifically, the element after the last character in the string). This line is assigning a null-character (really just a byte that is 0) to the address it points to. Strings in C don't store the length of the string, so the end is marked with a null character. Without doing this step, string-handling functions in C would run right through the end of the string and keep operating on memory that is allocated for something else.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Quote Originally Posted by sean View Post
    At this point, 'str' holds the address of the end of the string (specifically, the element after the last character in the string). This line is assigning a null-character (really just a byte that is 0) to the address it points to. Strings in C don't store the length of the string, so the end is marked with a null character. Without doing this step, string-handling functions in C would run right through the end of the string and keep operating on memory that is allocated for something else.
    but why isn't the \0 at the right position already? my while loop reads until a \0 is found. with *str = *ptr; why wouldn't \0 be covered too?

    thanks in advance!

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2,268
    my while loop reads until a \0 is found.
    Exactly. When it sees that character, the loop terminates and this bit of code inside the loop is not executed:

    *str = *ptr

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    ahhh kk finally got it, thanks every1!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 11-27-2011, 03:01 AM
  2. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 06:50 AM
  3. How to take a string input and remove whitespace?
    By Jasonx521 in forum C Programming
    Replies: 5
    Last Post: 10-06-2006, 10:24 PM
  4. Replies: 10
    Last Post: 06-20-2006, 03:07 PM
  5. '\r' signs misssing
    By Zahl in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2002, 09:52 AM

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