Author Topic: I need to take string from cin, split by comma into char array  (Read 3208 times)

0 Members and 5 Guests are viewing this topic.

Offline xMoD

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
I need to take string from cin, split by comma into char array
« on: November 01, 2014, 08:35:57 pm »
Code: [Select]

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 :



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

Offline Jano

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #1 on: November 01, 2014, 08:49:34 pm »
For the junk code, try this.
declare fin as
Code: [Select]
char fin[100]={0};

Offline xMoD

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #2 on: November 01, 2014, 09:05:32 pm »

For the junk code, try this.
declare fin as
Code: [Select]
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
« Last Edit: November 01, 2014, 09:07:00 pm by xMoD »

Offline xMoD

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #3 on: November 01, 2014, 10:22:55 pm »
Anyone at all, have anything.
I tried on another forum, but didnt get anything at all either!

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: I need to take string from cin, split by comma into char array
« Reply #4 on: November 02, 2014, 06:36:26 am »
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.
« Last Edit: November 02, 2014, 09:07:37 am by Xires »
-Xires

Offline L0aD1nG

  • Peasant
  • *
  • Posts: 83
  • Cookies: 6
  • NeverFear1isHere
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #5 on: November 23, 2014, 11:32:57 am »
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.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #6 on: December 20, 2014, 03:28:24 am »
You are writing C-like code, that's not the C++ way to do it.
Code: [Select]
#include <iostream>
#include <sstream>

int main()
{
  std::istringstream isstrm("one,two,three");
  {
    std::string tmp;
    while (getline(isstrm, tmp, ','))
    {
      std::cout << tmp << '\n';
    }
  }
}
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline Mytosus

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 2
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #7 on: October 05, 2015, 07:40:40 pm »
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

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #8 on: October 06, 2015, 11:04:50 am »
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
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: I need to take string from cin, split by comma into char array
« Reply #9 on: October 08, 2015, 06:13:43 am »
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.
-Xires

Offline novaccainne

  • Serf
  • *
  • Posts: 29
  • Cookies: 2
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #10 on: October 08, 2015, 08:35:14 pm »
Just for fun :)

Code: [Select]
```
 ~/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);
}

```
« Last Edit: October 08, 2015, 08:36:51 pm by novaccainne »

Offline chris_kzn

  • Serf
  • *
  • Posts: 25
  • Cookies: 2
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #11 on: October 10, 2015, 09:34:42 pm »
depending on the ide you are using, I have used the split() option before while using the QT ide

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #12 on: October 12, 2015, 09:57:49 pm »
Just for fun :)

Code: [Select]
```
 ~/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:
Code: [Select]
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.
« Last Edit: October 12, 2015, 10:02:01 pm by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline novaccainne

  • Serf
  • *
  • Posts: 29
  • Cookies: 2
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #13 on: October 13, 2015, 01:19:50 pm »
That realloc() call for each token found is really an ugly idea though..

Also your last couple lines could be this instead:
Code: [Select]
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 :)

Code: [Select]
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 :) 
« Last Edit: October 13, 2015, 01:50:42 pm by novaccainne »

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: I need to take string from cin, split by comma into char array
« Reply #14 on: October 15, 2015, 03:38:13 am »

Code: [Select]
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?
« Last Edit: October 15, 2015, 03:47:20 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]