Thread: Remove trailing whitespace

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Remove trailing whitespace

    The two approaches that come to mind:
    Code:
    char message[20];
    fgets(message, 20, stdin);
    int stringLen = strlen(message);
    while(stringLen > 0 && isspace (s[stringLen -1])){
    stringLen--;
    }
    message[stringLen] = '\0';
    and

    Code:
    char message[20];
    fgets(message, 20, stdin);
    int stringLen = strlen(message);
    while(message[stringLen] == ' ' || message[stringLen] == '\t'){
    stringLen--;
    }
    message[stringLen++] = '\0';
    I know I can just use the first approach but I'm wondering why the second approach isn't working, it doesn't remove any trailing whitespace.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,436
    Because isspace() also matches with \n, which is the typical last character of the line returned by fgets().
    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.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    30
    Quote Originally Posted by Salem View Post
    Because isspace() also matches with \n, which is the typical last character of the line returned by fgets().
    Does this match white space and newline at the same time. I am trying to understand what you meant by the quoted statement. Your explanation will be of help. Thanks

    Code:
    int main(void)
    {
    char space = '\t';
    if(isspace(space))
    {
    printf("Found a space\n\n");
    }
    
    return(0);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,436
    Did you run the program to find out?


    Have you read the manual page for isspace() ?
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,184
    The isspace() function tests for all whitespace, which includes the space, tab, newline, vertical tab, formfeed, and carriage return characters.

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    30
    Quote Originally Posted by Salem View Post
    Did you run the program to find out?


    Have you read the manual page for isspace() ?
    It prints out the message found space. While practicing this morning i read in a book about some of the function in ctype and i worked with isspace only to find you talking about it.
    I did not get your explanation right hence the i asked the question seeking explanation.

    I have not read the manual for isspace.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    30
    Quote Originally Posted by jimblumberg View Post
    The isspace() function tests for all whitespace, which includes the space, tab, newline, vertical tab, formfeed, and carriage return characters.
    Thanks got the explanation. Is there a link online to the manual pages for all functions in the ctype library.

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    535
    Quote Originally Posted by splitfunc0 View Post
    Thanks got the explanation. Is there a link online to the manual pages for all functions in the ctype library.
    Use the man system, on Linux, or online, to view ctype.h functions, and all C functions.

    You can also use Google to search "man isspace" or "man 3 isspace" for any function that can also be used on the command line or in a bash script, such as printf(), on Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. remove trailing blanks
    By shotakobakhidze in forum C Programming
    Replies: 4
    Last Post: 09-27-2015, 12:12 AM
  2. using sscanf to skip trailing whitespace
    By y_cant_i_C in forum C Programming
    Replies: 7
    Last Post: 09-25-2006, 06:01 PM
  3. Replies: 10
    Last Post: 06-20-2006, 03:07 PM
  4. Removing leading and trailing whitespace
    By JimpsEd in forum C Programming
    Replies: 2
    Last Post: 05-14-2006, 03:55 PM
  5. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM

Tags for this Thread