By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
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

P: 39
strtok is the beast that you are looking for and it is used in a similar fashion. reference
Mar 19 '07 #3

P: 5
zmp
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

Expert 100+
P: 1,510
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

P: 5
zmp
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.