I used strtok to split my string. say it returns a char* "abc", is there a way I can separate each character (so a,b,c)? I am trying to do so, so I can add each character separately into a different array.
Asked
Active
Viewed 561 times
-1
-
2All character strings are also character arrays separated by character. Just use an index to pick the one you want. Can you show an attempt or examples of what you're trying to achieve? Thanks. – ggorlen Feb 21 '20 at 06:37
-
I used strtok to split user input, and strtok returns a char*, in this case, char* x = "abc". Im trying to separate each character, so I can add that character into another array; for example, a will go to index 0 of the array, b goes to index 1, c goes to index 2. – Angelo Feb 21 '20 at 06:52
-
1`x` is already a pointer to an array of chars in your example. `x[0]` is already `'a'`, `x[1]` is `'b'`, etc. – ggorlen Feb 21 '20 at 07:27
2 Answers
-1
#include <string.h>
#define LARGE_LENGTH 50
int main()
{
char *s="abc"; // here memory allocation is done on stack something called as stringpool
// look here for more info in context of 'C' Programming //https://stackoverflow.com/questions/11399682/c-optimisation-of-string-literals
char d[LARGE_LENGTH];
for(int i-0;i<=strlen(s);i++)
{
d[i]=*s;
s++; // incremented memory address
}
//printf("whatever");
return 0;
}

cmgwins
- 14
- 2
-2
String str = "abc";
String tmp[] = new String[10];
for(int i = 0 i < str.length()-1; i++){
tmp[i] = str.substring(i,i+1);
}