but I've only 1 string (i.e. awwsa\n) !
And I need to find the position of \n (and thats what I'm doing).
And then replace it with a '\0' (isn't my code doing that?)
Where am I logically wrong?
Please explain. I really cannot understand this!
but I've only 1 string (i.e. awwsa\n) !
And I need to find the position of \n (and thats what I'm doing).
And then replace it with a '\0' (isn't my code doing that?)
Where am I logically wrong?
Please explain. I really cannot understand this!
If the newline is because of fgets it is right before the null character. So do what you said except instead of using strlen(string) you add a minus one.so if you got a string, "This is a string" using fgets then the array would be "T h i s i s a s t r i n g \n \0". This would take the string length which is 18 and then subtract one so that it is now on the \n and then replace it with \0.Code:string[strlen(string)-1);
~Sven
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
thanks a ton Sven. But is there any different assignment used for this case?
= seems not to work.
Is that the correct syntax? But thats failing!Code:ip[strlen(ip)-1]='\0';
ohh I again messed up with ==
Thanks
Nothing changes!
I use :
to call the fn and it prints the onld one with \nCode:teststr=get_rid_of_nl("Hello\n");
Code:n = strlen( str1 ) for ( i = 0; i < n; i++ ) { if ( str1[i] == '\n' ) { str1[i] = '\0'; } }
That code is raw, but that's the way you should be tackling the problem, in my opinion.
it's ironic considerate rarity patron of love higher knowledge engulfs me...
but strangely enough its not allowing that ip[i] = '\0' assignment!!!
please explain!
Are you by any chance passing a string literal? How about posting your full code, instead of just some random bits? Also "not working" doesn't tell us anything. Post a compilable example, complete with the input you give and the out put you get.
I hate having to teach people how to think for themselves.
Quzah.
Hope is the first step on the road to disappointment.
Maybe it's your compiler?
This runs fine on mine:
Code:int main(int argc, char *argv[]) { int i, n; char str1[] = "balls\nsackaroo\n"; n = strlen( str1 ); for ( i = 0; i < n; i++ ) { printf("%c", str1[i]); } for ( i = 0; i < n; i++ ) { if ( str1[i] == '\n' ) { str1[i] = '\0'; } } printf("\n\n"); for ( i = 0; i < n; i++ ) { printf("%c", str1[i]); } system("PAUSE"); return 0; }
Though, the null characters print as spaces.
it's ironic considerate rarity patron of love higher knowledge engulfs me...
Done it. Thanks a ton guys