Thread: how to remove newline from a string

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    how to remove newline from a string

    hi people.

    i tried looking for a function that chops the newline off of a string but couldnt find one so i went about writing my own function to do it.

    here is the code
    Code:
    /* removes the newline character from a string */
    char* chop(char *string)
    {
    	int i, len;
    	len = strlen(string);
    	char *newstring;
    
    	newstring = (char *)malloc(len-1);
    
    
    	for(i = 0; i < strlen(string)-1; i++)
    	{
    		newstring[i] = string[i]; 
    		printf("in the string %c\n", string[i]);
    	}
    
    	printf("len og newstring %d\n", strlen(newstring));
    	printf("string now .......... %s\n", newstring);
    
    	return newstring;
    }
    this doesnt seem to work because it keeps giving me a string which is longer than the len-1 size i require. it just seems to put garbage at the end of it.

    Any help is greatly appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you insist on creating a new string...
    Code:
    char *choppy( char *s )
    {
        char *n = malloc( strlen( s ? s : "\n" ) );
        if( s )
            strcpy( n, s );
        n[strlen(n)-1]='\0';
        return n;
    }
    Eh, looks right. Not good mind you, much better ways to do this, but amusing and if correct, sufficient. :beer:


    Quzah.

    edit
    Last edited by quzah; 10-01-2005 at 04:00 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    how did the '\n' get in the string? fgets() normally (but not always) puts one there. All you have to do is replace the last character in the string with a 0, after verifying that the last character is a newline.
    Code:
    char str[80];
    int len;
    ...
    fgets(str,sizeof(str),fp);
    // remove newline
    len = strlen(str);
    if( str[len-1] == '\n' )
        str[len-1] = 0;
    Last edited by Ancient Dragon; 10-01-2005 at 04:33 AM.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Ancient Dragon
    fgets() normally (but not always) puts one there.
    When doesn't it? On a non standard implementaion?

    fgets

    Declaration:

    char *fgets(char *str, int n, FILE *stream);

    Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. The newline character is copied to the string. A null character is appended to the end of the string.

    On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    possibly implementation defined -- this is from MSDN

    The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.
    google "man fgets"
    The fgets() function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a `\0' character is appended to end the string.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If there is a newline and there is room for it in the destination, then it is copied over.

  7. #7
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    You can do it lots of different ways..

    Code:
    if( (ptr = strchr(str, '\n')) != NULL)
        *ptr = '\0';

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I like to do this:
    Code:
    void chomp(char *s) {
        while(*s && *s != '\n' && *s != '\r') s++;
    
        *s = 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Like I said, there are many ways to do it. Here's one I function-wrapped, origionally posted by Prelude:
    Code:
    void choppy( char *s )
    {
        s[strcspn ( s, "\n" )] = '\0';
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  2. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM