470,596 Members | 1,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

How do i split a char array? (C++)

5
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.

Expand|Select|Wrap|Line Numbers
  1. 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? ;-)
Mar 19 '07 #1
5 66591
horace1
1,510 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. 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
Mar 19 '07 #2
lqdeffx
39
strtok is the beast that you are looking for and it is used in a similar fashion. reference
Mar 19 '07 #3
zmp
5
Thanks for the quick answers :-)

I tried an example and it did work out:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.     char str[] = "this is a test";
  4.     char* test;
  5.     test = strtok(str, " "); // Splits spaces between words in str
  6.  
  7.     printf ("%s\n",test); // Writes "this"
  8.     test = strtok (NULL, " ,.-");
  9.  
  10.     printf("\n%i\n",sizeof(test)); // Writes 4
  11.     system("pause");
  12.     return 0;
  13. }
  14.  
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?
Mar 19 '07 #4
horace1
1,510 Expert 1GB
Thanks for the quick answers :-)

I tried an example and it did work out:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.     char str[] = "this is a test";
  4.     char* test;
  5.     test = strtok(str, " "); // Splits spaces between words in str
  6.  
  7.     printf ("%s\n",test); // Writes "this"
  8.     test = strtok (NULL, " ,.-");
  9.  
  10.     printf("\n%i\n",sizeof(test)); // Writes 4
  11.     system("pause");
  12.     return 0;
  13. }
  14.  
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
Expand|Select|Wrap|Line Numbers
  1.     char str[] = "this is a test";
  2.     char *test[10];
  3.     test[0] = strtok(str, " "); // Splits spaces between words in str
  4.     printf ("%s\n",test[0]); // Writes "this"
  5.     test[1] = strtok (NULL, " ,.-");
  6.     printf ("%s\n",test[1]); // Writes "is"
  7.  
test needs to be an array of char*
Mar 19 '07 #5
zmp
5
is this what you are trying to do
Expand|Select|Wrap|Line Numbers
  1.     char str[] = "this is a test";
  2.     char *test[10];
  3.     test[0] = strtok(str, " "); // Splits spaces between words in str
  4.     printf ("%s\n",test[0]); // Writes "this"
  5.     test[1] = strtok (NULL, " ,.-");
  6.     printf ("%s\n",test[1]); // Writes "is"
  7.  
test needs to be an array of char*
yeah thats what i wanted to do, thank you :-)

This is what i ended up with:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3. char str[] = "this is a test";
  4.     char *test[sizeof(strtok(str, " "))];
  5.     if(sizeof(test) > 0)
  6.     {
  7.         test[0] = strtok(str, " "); // Splits spaces between words in str
  8.         printf ("%s\n",test[0]); // Writes "this"
  9.         for(int i = 1; i < sizeof(strtok(str, " ")); i++)
  10.         {
  11.             test[i] = strtok (NULL, " ,.-");
  12.             printf ("%s\n",test[i]); // Writes "is"
  13.         }
  14.     }
  15.  
  16.     system("pause");
  17.     return 0;
  18. }
  19.  
Anyways, thanks for all the replies. I appriciate that :-)
Mar 19 '07 #6

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
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
By using this site, you agree to our Privacy Policy and Terms of Use.