PDA

View Full Version : String to Array (like PHP's explode)


webdogjcn
06-27-2006, 08:22 PM
Is there any way I can convert a string into a single dimensional array like PHP's explode function? So suppose:
str_given = hey my name is blah
array_new = explode(str_given)

and then array_new would look like:
[hey]
[my]
[name]
[is]
[blah]

Brandoe85
06-27-2006, 08:33 PM
What language? :confused:
Most lanuages have a split() function that returns an array.

Good luck;

webdogjcn
06-27-2006, 08:41 PM
oh whoops sorry C++

Brandoe85
06-27-2006, 08:46 PM
Well, I don't know c++, but google turned up some good results:

http://www.google.com/search?hl=en&q=c%2B%2B+split&btnG=Google+Search

Looks like it's not a built in function, but it's been done.

Good luck;

rpgfan3233
06-27-2006, 10:09 PM
Do you mean something like:

string x = "HELLO WORLD!";
and you want that to be an array?

Just use the c_str() member function:

#include <cstdio> // iostream in MinGW is too large to include
#include <string>

int main () {
char* charArray = (char*)calloc(256, sizeof(char)); // 255 elements in the array because the last one is a NULL terminator
std::string x = "HELLO WORLD!";

charArray = (char*)x.c_str();
charArray[255] = '\0'; // if there are too many characters and it doesn't halt the program, the string is truncated here

printf("%s\n", charArray);

return 0;
}


On the other hand, if you are referring to a string as in an ASCIIZ string, where the last character is the null terminator ('\0'). This is a character array in C/C++. In terms of an array, it would look like this: {'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!', '\0'}. Of course, because of calloc(), every unfilled character is a null terminator (if it were an integer array, it would be a 0). Note that a bit of error checking is still performed. Instead of "HELLO WORLD!" you could have the string "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" (repeated 4 times with no spaces between them) entered by a user in a single line, assuming you used user input. This would cause an issue with memory as you would be overwriting memory that you didn't allocate. To ensure that messed up things would not be printed, the string is truncated.

nytrokiss
06-27-2006, 10:57 PM
a string is an array of chars please explain even when you code like this you still have and array #include stdio.h int main () { char *string = " hello world"; int a = 0; for(a=0;a<sizeof(string);a++) { printf(string[a]) } return 0;

}

see it's a string of chars in a memory location and all you are doing is going through it!

oracleguy
06-27-2006, 11:59 PM
Explode in PHP splits up the string based on a passed in character and returns an array of strings that have been broken up by that character that is what the OP is going for.

You could use the strtok() function to do what you want too. And thats built in. However it works with C-strings and not the string class.

rpgfan3233
06-28-2006, 04:53 AM
Yeah, if you want to use the string class, you'll need to invoke the c_str() member function. A good page to see how it can be used:
http://cplusplus.com/ref/cstring/strtok.html

Note that if you have a string class variable (e.g. "string x" instead of "char *x" and instead of "char x[SOME_INTEGER]"), you'll need to use "x.c_str()" instead of just "x":

strtok(x.c_str(), delimiters);

If the delimiter string is a string class variable also, then you must do the same to that.