473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 69213
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

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: brianshields | last post by:
array("a", "b", "c", "d", "e"); I want to now be able to load "b" into a variable ($var) then print it out print $var thanks!
3
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; ...
5
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
8
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc =...
2
by: AMP | last post by:
Hello, I am trying to split an Array into another array. Each value in the array has tab delimited strings. I am getting: Cannot implicitly convert type 'string' to 'string' I am trying...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.