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!
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!
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...
I'm trying this...
I want to overwrite \n with \0 so that I can remove it.Code:char *get_rid_of_nl(char *ip) { int len = strlen(ip); ip[len]='\0'; return ip; }
Why isn't this workin?
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.
Don't know what function to use
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
Scan the string.
Loop throw it until you find '\n' or '\0' and overwrite with '\0'.
Originally Posted by AngKar
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...
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.
I know the logic
I wanted the syntax!
Anyways no more posts...
Sorry if I've bothered u!
As an alternative plz suggest some good C programming (esp pointers) url
Originally Posted by AngKar
So, you wanted us to write the code for you?
it's ironic considerate rarity patron of love higher knowledge engulfs me...
not entirely - i expect you can correct my faults...again i see the follwoing code doesn't work:
why? could you please explain without pulling my legs?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 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.
Originally Posted by AngKar
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...