remove newline character from a string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

remove newline character from a string

 
0
  #1
May 5th, 2009
hi all.

i have read a text line from a file using
  1. fgets()
into a char array
  1. char astr[30]
then i did
  1. strcat(astr,"secondstring")

The output of
  1. printf (astr)
is appearing on 2 lines instead on same line.

plz advise me wat i did wrong.
thanks.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: remove newline character from a string

 
0
  #2
May 5th, 2009
fgets places '\n' char at the end of string. Overwrite it by zero byte (end of C-string) - that's all:
  1. if (fgets(astr,sizeof astr,f)) {
  2. char* p = strchr(astr,'\n');
  3. if (p)
  4. *p = '\0';
  5. ...
  6. } else { /* eof or i/o error */
  7. ...
  8. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: remove newline character from a string

 
2
  #3
May 5th, 2009
>fgets places '\n' char at the end of string.
To the OP: ArkM's code does the right thing, but the quoted statement is incomplete. fgets preserves the newline, but not if the buffer is filled. So solutions like the following distressingly common one are incorrect:
  1. if ( fgets ( buf, sizeof buf, stdin ) != NULL ) {
  2. buf[strlen ( buf ) - 1] = '\0';
  3.  
  4. /* ... */
  5. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: remove newline character from a string

 
0
  #4
May 5th, 2009
ArkM's method is sufficient, but i prefer this way.

  1. buf[strcspn(buf,'\n')] = '\0';

it's just a single line of code that will replace the newline with a NULL, if a newline is found, or will do nothing if one is not there

.
Last edited by jephthah; May 5th, 2009 at 11:29 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: remove newline character from a string

 
2
  #5
May 5th, 2009
>ArkM's method is sufficient, but i prefer this way.
Keep in mind that of the potential ways to do this, strcspn is among the least efficient while strchr is among the most efficient.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: remove newline character from a string

 
0
  #6
May 5th, 2009
oh, i wasnt aware of that.

i guess the only time i ever concern myself with reducing cycle times is in embedded applications, and there i rarely deal with strings and newlines.

but thats useful to know. thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: remove newline character from a string

 
2
  #7
May 5th, 2009
I prefer using strcspn myself, but knowing the performance characteristics is important. However, none of the methods for stripping a newline will change the fact that the whole operation is I/O bound because of fgets. You can save cycles when stripping the newline, but it isn't likely to have a noticeable effect on performance as a whole.

Just food for thought.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: remove newline character from a string

 
0
  #8
May 6th, 2009
Narue,
Because fgets() reads a line up to the \n and loads everything up to the RETURN, isn't it simply a matter of testing the last character in the buffer with \n and if it isn't, they entered more than the requested data? Simple use of strlen() and a test?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: remove newline character from a string

 
3
  #9
May 6th, 2009
Originally Posted by WaltP View Post
Narue,
Because fgets() reads a line up to the \n and loads everything up to the RETURN, isn't it simply a matter of testing the last character in the buffer with \n and if it isn't, they entered more than the requested data? Simple use of strlen() and a test?
Yes, what's your point? If all you want to do is determine if a partial line was read, then looking for a newline is trivial. Ideally one would write code that can handle both cases where fgets reads a newline and where it doesn't. For example:
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. char buffer[5]; // Arbitrary small size
  6. char *s = NULL;
  7. size_t n = 0;
  8.  
  9. while ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
  10. size_t len = strlen ( buffer );
  11. char *save = realloc ( s, n + len + 1 );
  12.  
  13. if ( save == NULL ) {
  14. perror ( "Insufficient memory" );
  15. break;
  16. }
  17.  
  18. strcpy ( save + n, buffer );
  19. s = save;
  20. n += len;
  21.  
  22. if ( buffer[len - 1] == '\n' ) {
  23. s[n - 1] = '\0';
  24. break;
  25. }
  26. }
  27.  
  28. printf ( ">%s<\n", s != NULL ? s : "(null)" );
  29. free ( s );
  30.  
  31. return 0;
  32. }
But if you're not worrying about long lines, you still need to consider the case where a newline may not be present, which is what I was talking about with the usual naive strlen solution:
  1. buffer[strlen ( buffer ) - 1] = '\0';
Unless this is qualified with a test to verify that a newline is present at that index, you would be removing valid data. An extra test is needed to make it work for long lines:
  1. size_t last = strlen ( buffer ) - 1;
  2.  
  3. if ( buffer[last] == '\n' )
  4. buffer[last] = '\0';
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: remove newline character from a string

 
0
  #10
May 6th, 2009
>Simple use of strlen() and a test?
strlen() has to itinerate through the string to give the length, to that
the check has to be performed to find that '\n'.

strchr() has to itinerate through the string looking for the given char.
Hard for me to see which is faster.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 2294 | Replies: 20
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC