Thread: getting rid of newline

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    but I've only 1 string (i.e. awwsa\n) !
    And I need to find the position of \n (and thats what I'm doing).
    And then replace it with a '\0' (isn't my code doing that?)
    Where am I logically wrong?
    Please explain. I really cannot understand this!

  2. #17
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    If the newline is because of fgets it is right before the null character. So do what you said except instead of using strlen(string) you add a minus one.
    Code:
    string[strlen(string)-1);
    so if you got a string, "This is a string" using fgets then the array would be "T h i s i s a s t r i n g \n \0". This would take the string length which is 18 and then subtract one so that it is now on the \n and then replace it with \0.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #18
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    thanks a ton Sven. But is there any different assignment used for this case?
    = seems not to work.
    Code:
    ip[strlen(ip)-1]='\0';
    Is that the correct syntax? But thats failing!

  4. #19
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    ohh I again messed up with ==
    Thanks

  5. #20
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Nothing changes!
    I use :
    Code:
    teststr=get_rid_of_nl("Hello\n");
    to call the fn and it prints the onld one with \n

  6. #21
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    n = strlen( str1 )
    
    for ( i = 0; i < n; i++ )
    {
        if ( str1[i] == '\n' )
        {
             str1[i] = '\0';
        }
    }

    That code is raw, but that's the way you should be tackling the problem, in my opinion.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #22
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    but strangely enough its not allowing that ip[i] = '\0' assignment!!!
    please explain!

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you by any chance passing a string literal? How about posting your full code, instead of just some random bits? Also "not working" doesn't tell us anything. Post a compilable example, complete with the input you give and the out put you get.

    I hate having to teach people how to think for themselves.


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

  9. #24
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Maybe it's your compiler?

    This runs fine on mine:
    Code:
    int main(int argc, char *argv[])
    {
        int i, n;
        char str1[] = "balls\nsackaroo\n";
        
        n = strlen( str1 );
        
        for ( i = 0; i < n; i++ )
        {
            printf("%c", str1[i]); 
        }
        
        
        for ( i = 0; i < n; i++ )
        {
            if ( str1[i] == '\n' )
            {
                 str1[i] = '\0';
            }
        }
        
        printf("\n\n");
        
        for ( i = 0; i < n; i++ )
        {
            printf("%c", str1[i]); 
        }
        
        
        system("PAUSE");	
        return 0;
    }

    Though, the null characters print as spaces.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #25
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Done it. Thanks a ton guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 07:50 PM
  2. Replies: 19
    Last Post: 09-17-2005, 09:49 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM
  5. newline / memset
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-31-2001, 01:21 AM