EvilZone
Programming and Scripting => C - C++ => : xMoD November 01, 2014, 08:35:57 PM
-
char *split(string s)
{
char holder[100]; // Holder for string to char[]
char fin[100]; // Final array
char * pch;
strcpy(holder, s.c_str());
pch = strtok(holder, " ");
while (pch != NULL)
{
printf("=>%s\n", pch);
strcpy(fin, pch); // copy pch into fin array
pch = strtok(NULL, " ");
}
return fin;
}
Is my current code, and I use like:
string cmd;
getline(cin,cmd);
cout << split(cmd)<<endl;
It spits out each thing i type on a new line, seperated by a space...
But when I cout it, its a jumble of junk :
(http://i.imgur.com/fMtdKwS.png)
Is there a better way to do this, and where if i did
split(cmd)[0] the first element would be the first word separated by a space OR comma.. (Or any other deliminator i want)
Thanks :D
-
For the junk code, try this.
declare fin as
char fin[100]={0};
-
For the junk code, try this.
declare fin as
char fin[100]={0};
didnt change at all :/
Doesnt anyone know how to do what I need :/
take string from cin split by comma into char array
At least without using boost
-
Anyone at all, have anything.
I tried on another forum, but didnt get anything at all either!
-
For C:
The specific issue you're having is due to returning a local pointer. You may want to pass the pointer(fin, in your case) to the function so that it has a higher scope or you can have split() malloc() the memory necessary for the 'fin' array and then free() the memory in your calling function after you're done with it.
For C++:
Look up stringstreams. You may also want to look into functions for searching strings for specific characters and substrings. Also, you may consider trying to use references to pass a pre-allocated block of memory to split(). Alternatively, you can use 'new' and 'delete' but I don't really recommend using those in staggered scope.
Also..pick one..either C or C++.
If you need further help, just ask and try to be specific. I'll try to help you as much as possible without just giving you the answer.
-
On C usually this happens when you are trying to print an char array as a string which doesn't end with '\0'.
Might this is the problem here too.
-
You are writing C-like code, that's not the C++ way to do it.
#include <iostream>
#include <sstream>
int main()
{
std::istringstream isstrm("one,two,three");
{
std::string tmp;
while (getline(isstrm, tmp, ','))
{
std::cout << tmp << '\n';
}
}
}
-
Go through each character in the string. Replace commas with 0 and store the index in an array. create the array to store the chars. set a pointer to the beginning of the string and store in the array. add the index stored in an array to pointer then store pointer into the array... and so on
-
Go through each character in the string. Replace commas with 0 and store the index in an array. create the array to store the chars. set a pointer to the beginning of the string and store in the array. add the index stored in an array to pointer then store pointer into the array... and so on
Code? Because i don't get half of what you are saying...
And i think that is alot of work, not a good algorithm design IMHO. But lets see your code for this
-
Actually, Mytosus's approach is valid, except that the "array to store the chars" should store pointers to char instead. Given the base of an array, the index stored in the first array acts as an offset. The first string will be at offset 0, the next at *(baseAddressOfArray + arrayOfIndices[1]), then *(baseAddressOfArray + arrayOfIndices[2]), and so on. The char* array can store pointers to these locations directly, effectively producing an array of C-style strings.
-
Just for fun :)
```
~/fun cat test.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char** split(char *s,const char *delim) {
int delims = 0; char *p = strtok(s,delim), **res = NULL;
while(p) {
res = realloc(res,sizeof(char*) * ++delims); // allocating memory
if ( res == NULL ) exit(-1); // there is no more memory , allocation is failed
res[delims-1] = p;
p = strtok(NULL,delim);
}
// adding last element because of the null
res = realloc(res,sizeof(char*) * ++delims);
res[delims-1] = 0;
return res;
}
int main () {
char buf[] ="it is just a simple example for fun";
char** data = split(buf," ");
for(int i=0; data[i]; i++)
printf("%d = %s\n",i, data[i] );
free(data);
}
```
-
depending on the ide you are using, I have used the split() option before while using the QT ide
-
Just for fun :)
```
~/fun cat test.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char** split(char *s,const char *delim) {
int delims = 0; char *p = strtok(s,delim), **res = NULL;
while(p) {
res = realloc(res,sizeof(char*) * ++delims); // allocating memory
if ( res == NULL ) exit(-1); // there is no more memory , allocation is failed
res[delims-1] = p;
p = strtok(NULL,delim);
}
// adding last element because of the null
res = realloc(res,sizeof(char*) * ++delims);
res[delims-1] = 0;
return res;
}
int main () {
char buf[] ="it is just a simple example for fun";
char** data = split(buf," ");
for(int i=0; data[i]; i++)
printf("%d = %s\n",i, data[i] );
free(data);
}
```
That realloc() call for each token found is really an ugly idea though..
Also your last couple lines could be this instead:
res = realloc(res, sizeof(char *) * delims + 1;
res[delims] = 0;
No need to increment and store the value of delims + 1 in delims if you are going to subtract from it afterwards.
-
That realloc() call for each token found is really an ugly idea though..
Also your last couple lines could be this instead:
res = realloc(res, sizeof(char *) * delims + 1;
res[delims] = 0;
No need to increment and store the value of delims + 1 in delims if you are going to subtract from it afterwards.
Yeah, you solved a big mistery :)
printf("%s",( 0xff | 0xf1 == 110 | 011 == ((int)true) == (int)!false || 0 | 0 ) ? "Yeah that was a good feedback ..." : "I made a mistake");
.. but thank you for the feedback :)
-
printf("%s",( 0xff | 0xf1 == 110 | 011 == ((int)true) == (int)!false || 0 | 0 ) ? "Yeah that was a good feedback ..." : "I made a mistake");
O.o... What is the point of this?
-
Hi,
Nothing, I thought you wanted to tease me :)
O.o... What is the point of this?