Learner Question RE: removing newlines 
Author Message
 Learner Question RE: removing newlines

how can i remove a newline (\n) from a string ?

--
All thought welcome,

caf

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Sun, 18 Nov 2001 03:00:00 GMT  
 Learner Question RE: removing newlines

Quote:

> how can i remove a newline (\n) from a string ?

> --
> All thought welcome,

> caf

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Do you want to remove it or replace it?

Replace is easy.

To remove it:
    (1) find it.
    (2) move all chars forward 1

for ( i = 0; string[i] != '\0'; i++ )
    if ( string[i] == '\n' )
        /* do your thing */

- johnk



Mon, 19 Nov 2001 03:00:00 GMT  
 Learner Question RE: removing newlines

Quote:


>> how can i remove a newline (\n) from a string ?

>> --
>> All thought welcome,

>> caf

>> Sent via Deja.com http://www.deja.com/
>> Share what you know. Learn what you don't.

>Do you want to remove it or replace it?

>Replace is easy.

>To remove it:
>    (1) find it.
>    (2) move all chars forward 1

Assuming that the string is writable. To ensure a correct answer the original
poster needs to give a lot more context.

--
-----------------------------------------


-----------------------------------------



Mon, 19 Nov 2001 03:00:00 GMT  
 Learner Question RE: removing newlines
if the '\n' is the last character before the '\0'
... usually the case when you have taken the input using
fgets ();

try this
stringName[strlen(stringName)-1] = '\0';

Quote:

> how can i remove a newline (\n) from a string ?

> --
> All thought welcome,

> caf

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.



Sun, 25 Nov 2001 03:00:00 GMT  
 Learner Question RE: removing newlines
The standard way to do this is:

char * p = strchr(buf, '\n');
if (p)
    *p = '\0';

Quote:

>if the '\n' is the last character before the '\0'
>... usually the case when you have taken the input using
>fgets ();

>try this
>stringName[strlen(stringName)-1] = '\0';


>> how can i remove a newline (\n) from a string ?

>> --
>> All thought welcome,

>> caf

>> Sent via Deja.com http://www.deja.com/
>> Share what you know. Learn what you don't.



Sun, 25 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Remove newlines from string

2. Newbie C Learner.. a few questions

3. Question from a new learner

4. New Learner Has Questions

5. Learner Question reg.

6. Learner Question reg. code/compile

7. A new C learner's question

8. A Stupid Learner Question (about CEdit)

9. Questions about newline

10. CEdit question (newline problem)

11. utter learner help please

 

 
Powered by phpBB® Forum Software