Hey
First of, c++ aint my primary language, just started using it (again) two weeks ago.. Anyways, i tried searching around on google for a method to split a char* and return the first word in the array. - char* test = "hello this is a test";
In C#, i would use Regex.Split(test, " ")[0] to get the first word ("hello"), but i dont know how to do that in C++.
Anyone who can help me out? ;-)
5 66591
Hey
First of, c++ aint my primary language, just started using it (again) two weeks ago.. Anyways, i tried searching around on google for a method to split a char* and return the first word in the array. - char* test = "hello this is a test";
In C#, i would use Regex.Split(test, " ")[0] to get the first word ("hello"), but i dont know how to do that in C++.
Anyone who can help me out? ;-)
you can use a string tokenizer, see
http://www.thescripts.com/forum/thread607870.html
strtok is the beast that you are looking for and it is used in a similar fashion. reference
Thanks for the quick answers :-)
I tried an example and it did work out: -
int main(int argc, char *argv[])
-
{
-
char str[] = "this is a test";
-
char* test;
-
test = strtok(str, " "); // Splits spaces between words in str
-
-
printf ("%s\n",test); // Writes "this"
-
test = strtok (NULL, " ,.-");
-
-
printf("\n%i\n",sizeof(test)); // Writes 4
-
system("pause");
-
return 0;
-
}
-
However, when i try and print test[1] (which i thought would be self explainary to do), i get an error "C2664" about that i cant " convert parameter 1 from 'char' to 'const char *' ".
How do i convert it?
Thanks for the quick answers :-)
I tried an example and it did work out: -
int main(int argc, char *argv[])
-
{
-
char str[] = "this is a test";
-
char* test;
-
test = strtok(str, " "); // Splits spaces between words in str
-
-
printf ("%s\n",test); // Writes "this"
-
test = strtok (NULL, " ,.-");
-
-
printf("\n%i\n",sizeof(test)); // Writes 4
-
system("pause");
-
return 0;
-
}
-
However, when i try and print test[1] (which i thought would be self explainary to do), i get an error "C2664" about that i cant " convert parameter 1 from 'char' to 'const char *' ".
How do i convert it?
is this what you are trying to do -
char str[] = "this is a test";
-
char *test[10];
-
test[0] = strtok(str, " "); // Splits spaces between words in str
-
printf ("%s\n",test[0]); // Writes "this"
-
test[1] = strtok (NULL, " ,.-");
-
printf ("%s\n",test[1]); // Writes "is"
-
test needs to be an array of char*
is this what you are trying to do -
char str[] = "this is a test";
-
char *test[10];
-
test[0] = strtok(str, " "); // Splits spaces between words in str
-
printf ("%s\n",test[0]); // Writes "this"
-
test[1] = strtok (NULL, " ,.-");
-
printf ("%s\n",test[1]); // Writes "is"
-
test needs to be an array of char*
yeah thats what i wanted to do, thank you :-)
This is what i ended up with: -
int main(int argc, char *argv[])
-
{
-
char str[] = "this is a test";
-
char *test[sizeof(strtok(str, " "))];
-
if(sizeof(test) > 0)
-
{
-
test[0] = strtok(str, " "); // Splits spaces between words in str
-
printf ("%s\n",test[0]); // Writes "this"
-
for(int i = 1; i < sizeof(strtok(str, " ")); i++)
-
{
-
test[i] = strtok (NULL, " ,.-");
-
printf ("%s\n",test[i]); // Writes "is"
-
}
-
}
-
-
system("pause");
-
return 0;
-
}
-
Anyways, thanks for all the replies. I appriciate that :-)
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
7 posts
views
Thread by brianshields |
last post: by
|
3 posts
views
Thread by s.subbarayan |
last post: by
|
15 posts
views
Thread by Kueishiong Tu |
last post: by
|
15 posts
views
Thread by Kueishiong Tu |
last post: by
|
5 posts
views
Thread by eagle_jyjh |
last post: by
|
4 posts
views
Thread by =?Utf-8?B?cm9nZXJfMjc=?= |
last post: by
|
13 posts
views
Thread by Superman859 |
last post: by
|
8 posts
views
Thread by Frank Liebelt |
last post: by
|
2 posts
views
Thread by AMP |
last post: by
|
5 posts
views
Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= |
last post: by
| | | | | | | | | | |