Removing '\n' From a String

This is a discussion on Removing '\n' From a String within the C Programming forums, part of the General Programming Boards category; Simple question, I feel real dumb asking this: How do you remove the "\n" from the end of a string ...

  1. #1
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7

    Question Removing '\n' From a String

    Simple question, I feel real dumb asking this:

    How do you remove the "\n" from the end of a string to prevent the printf to go to the next line?

    Example:
    Code:
    char a[]="testing\n";
    char b[]="123\n";
    
    printf("%s %s",a,b);
    From printing this:
    testing
    123

    To printing this:
    testing 123


    It may be from lack of sleep, but I'm just not seeing it...

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    displace the null back one character in each string, effectively stoping the string before the carriage return whilst replacing it...
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7
    ok, thanks

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    small code sample similar to what you are trying to do.
    Code:
    char* newgets(char* buffer,int num)
    {
    int i;
    fgets(buffer,num,stdin);
    i=strlen(buffer)-1;
    if (buffer[i]=='\n') buffer[i]='\0';
    return buffer;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7
    I figured out what I was doing wrong, and that's very similar to the code I used to do it. Thanks for the help though.

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. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Removing junk from end of string
    By dirkduck in forum C++ Programming
    Replies: 7
    Last Post: 05-20-2002, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21