Hello,
I was wondering if there is a way to only get a certain part of text from a variable. For example if y = hello hey sup howdy, is there any easy way to choose and print one of those varialbes? Thanks!
Hello,
I was wondering if there is a way to only get a certain part of text from a variable. For example if y = hello hey sup howdy, is there any easy way to choose and print one of those varialbes? Thanks!
i dint get u. can u elaborate on it.
ok,
I have a varialbe that is the following:
y = -1 hello hey sup -2 bye cya
Is there a way that I can get it to print everything except everything after -2?
Sure, you can use strstr() to find the bits of strings which interest you.
Code:char test[] = "this is a message"; char *p = strstr( test, "mess" ); if ( p != NULL ) { len = p - test; printf( "%.*s\n", len, test ); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thats almost what I need, Salem. But how would I want to include everything [I]after[I] "message"?
printf("%s",p) ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Probably more like
Code:printf("%s", p+strlen("message "));
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
may be this,just a matter of space .Originally Posted by dwks
printf("%s", p+strlen("message"));
No, since your way would print a space in front of p.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
c compiler ignores white spaces like , p.but strlen will give 1 more character for space in string.so i think ur will eat a character after message
It does, but not in strings. Check a binary dump if you like (or use gcc -S).c compiler ignores white spaces . . .
Not if the message isstrlen will give 1 more character for space in string.so i think ur will eat a character after message
Then your code would produce a pointer to a string that looks like this:Code:"this is the message and a long one at that"
Wheras mine would point toCode:" and a long one at that"
Do you see?Code:"and a long one at that"
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.