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!
Printable View
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 );
}
Thats almost what I need, Salem. But how would I want to include everything [I]after[I] "message"?
printf("%s",p) ?
Probably more like
Code:printf("%s", p+strlen("message "));
may be this,just a matter of space :p .Quote:
Originally Posted by dwks
printf("%s", p+strlen("message"));
No, since your way would print a space in front of p. :)
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).Quote:
c compiler ignores white spaces . . .
Not if the message isQuote:
strlen 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"