is it possible to extract parts of strings. ex:
say you have an inputted value for a character string:
something,joe
Can you get a code which will find the ',' in the string and extract anything after it?
is it possible to extract parts of strings. ex:
say you have an inputted value for a character string:
something,joe
Can you get a code which will find the ',' in the string and extract anything after it?
A non-STL method would be to use strchr() to find the comma, then increment the returned pointer by one.
If it worked OK, p will now point to the second part of the string.Code:p = strchr(myString, ','); if (p != NULL) p++;
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
it says "undefined symbol 'p' so what should i put
Is your string a c++ string or a char array?
My example was for the latter, and therefore p should be defined as
>char *p;
Code:#include <iostream> using namespace std; int main(void) { char s[] = "sentence,more"; char *p; if ((p = strchr(s, ',')) != NULL) { p++; cout <<p <<endl; } }
Last edited by Hammer; 09-15-2002 at 11:38 AM.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
could i get the code to extract before the comma. Only one comma, not like what vVv just posted
do a board search for strtok() or look it up in your helpfiles.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
can someone please post the code to extract 'joe' comma for this expression:
joe,something
could someone post the code please?
If you had done as I suggested you would have found many code samples similar to this.....
You will also know how to tailor this sort of code to your needs because you have read your helpfiles and know how strtok() works now.Code:#include <stdio.h> #include <string.h> int main ( ) { char test_msg[]="This is a test message"; char words[10][20]; // 10 words, up to 20 chars char *p; char *delim = " "; int i = 0, j; p = strtok(test_msg,delim); while ( p ) { strcpy( words[i], p ); p = strtok( NULL, delim ); i++; } printf ( "There are %d words\n", i ); for ( j = 0 ; j < i ; j++ ){ printf( "'%s'\n", words[j] ); } return 0; }
Still bemused??
Then there's always politics!
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Following Stoned_Coder:
The code that the others have posted is more elegant and easier to maintain, but this is another example.Code:#include <iostream> #include <cstdlib> #include <string> int main(void) { std::string a_string; std::string sub_string; int n; a_string = "I really like Joe, something or other."; std::cout << a_string; n = a_string.find("Joe,"); sub_string = a_string.substr(n, 4); std::cout << "\n\n" << sub_string << std::endl << std::endl; system("PAUSE"); return 0; }
Okay, niroopan, enough of our code. Let's see some of yours in the future and some specific questions beyond what you need to complete your assignment(s).
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
Another one for the pot, slightly shorter, doing the same as Stoner_Coders, but only doing the one comma. (Maybe easier to understand).Originally posted by niroopan
can someone please post the code to extract 'joe' comma for this expression:
joe,something
could someone post the code please?
Code:#include <iostream> using namespace std; int main(void) { char buf[50] = "joe,something"; strtok(buf, ","); cout <<buf <<endl; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]