getting rid of newline

This is a discussion on getting rid of newline within the C Programming forums, part of the General Programming Boards category; Hi, Could anyone suggest any good algo to get rid of a newline from a string? I've a variable as: ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    getting rid of newline

    Hi,

    Could anyone suggest any good algo to get rid of a newline from a string?
    I've a variable as:

    asas\n
    asad\n
    asar\n
    .
    .
    .

    I want to print it as :
    asas\tasad\tasar....

    But due to that newline I cannot!

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Scan the input one character at a time and check for newlines. If there is a newline, print nothing (or the tab, or whatever you want), otherwise print the character.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    I'm trying this...
    Code:
    char *get_rid_of_nl(char *ip)
    {
    	int len = strlen(ip);
    	ip[len]='\0';
    	return ip;
    }
    I want to overwrite \n with \0 so that I can remove it.

    Why isn't this workin?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because you're already pointing at the null character, and all you're doing is setting it back to what it is already. How about doing what you were told, and scanning through the string to find the newline?


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

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Don't know what function to use

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Aw come on! You have 8 threads or so over the course of a
    day or two! I am not the first person to say this but stop
    creating new threads for every little problem - reuse old threads.

    I am conflicted as to whether i should help because you need to
    learn that programming is about finding answers to problems by
    experiment and research - not by being told the solution to every
    possible task. Also, others have asked this and i haven't seen
    an answer - what is your program supposed to be doing? I am
    asssuming that your numerous threads are inter-related, because
    you posted the same question here which violates board rules

    I am in a forgiving mood, primarily because what you ask is quite
    trivial so i'll show you this:

    1) using strchr

    2) use a loop to iterate through each element of the string, and
    test it to see if it is the newline character. If it is, change it to
    '\0'.

    Now if you don'y know how to use a loop, what an element of
    a string is, or what the significance of '\0' is, then go here or
    here and quit wasting your own time.

    Perseverence is a quality even the most uninitiated programmer
    must have, to have any chance of writing a program themselves.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Scan the string.
    Loop throw it until you find '\n' or '\0' and overwrite with '\0'.

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by AngKar
    Don't know what function to use


    I'd suggest a for() loop.
    Last edited by bivhitscar; 04-28-2006 at 07:02 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always write your own. Don't you know what a loop is?

    [edit]
    Wow, beat three times. Like this dead horse of a thread...
    [/edit]


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

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    I know the logic
    I wanted the syntax!
    Anyways no more posts...
    Sorry if I've bothered u!

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    As an alternative plz suggest some good C programming (esp pointers) url

  12. #12
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by AngKar
    I know the logic
    I wanted the syntax!

    So, you wanted us to write the code for you?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    not entirely - i expect you can correct my faults...again i see the follwoing code doesn't work:
    Code:
    char *get_rid_of_nl(char *ip){
    char * pch;
    	int pos;
    	pch=strchr(ip,'\n');
    	pos = pch-ip+1;
    	ip[pos]='\0';
    	return ip;
    }
    why? could you please explain without pulling my legs?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use a loop? You really like to overcomplicate things, don't you? Just dereference 'pch' if it's not null, and assign it the null character.


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

  15. #15
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by AngKar
    why? could you please explain without pulling my legs?

    There is no loop in your code, how do you expect it to read through every element? I think you are making this more complicated than it needs to be.

    [EDIT]
    This time, I am the beaten one
    [/EDIT]
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Page 1 of 2 12 LastLast
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, 06: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, 09:46 AM
  4. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 12:41 PM
  5. newline / memset
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-31-2001, 12:21 AM

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