Thread: Remove new line (/n) from string

  1. #1
    Registered User
    Join Date
    Jan 2006

    Remove new line (/n) from string

    Hi...can someone please help me with the above as its preventing my strstr function from performing correctly....any pointers much appreciated as I am a beginner....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    char *p = strchr( buff, '\n' ) if ( p ) *p = '\0';

    If you're reading input using fgets(), this is all discussed in the FAQ

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    West Virginia
    Quote Originally Posted by Salem
    char *p = strchr( buff, '\n' ) if ( p ) *p = '\0';
    Just a minor nit... A missing ';', it should read
    char *p = strchr( buff, '\n' ); if ( p ) *p = '\0';
    Salem knows this, it's just a typo...
    Insert obnoxious but pithy remark here

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    And don't forget <string.h>.

    Here's the relevant FAQ entry:

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Search with command line arguments
    By goron350 in forum C Programming
    Replies: 5
    Last Post: 11-29-2004, 05:56 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM