Thread: Removing null terminator from a string

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Removing null terminator from a string

    Hey all,

    I'm new to C and am stuck...

    I have written the following code:
    ...
    char * token[32];
    token[0] = strtok(input," ");
    char * p;
    int i = 1;
    while (p = strtok(NULL, " "))
    token[i++] = p;
    ...
    and would like to remove the \n character from token[i-1].

    Anyone know how to do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,234
    strtok(NULL, " \n")
    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 2007
    Posts
    52
    Nice! I actually didn't think of that at all.

    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,009
    It seems that you've got a couple of terms confused:

    '\n' is a newline character.
    '\0' is a null terminator (or null byte, or null character).

    A null terminator marks the end of a string (without a null terminator, you don't have a string). A newline marks the end of a line, and of course isn't a necessary part of a string the way a null terminator is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM