Aug 1, 2001 #1 OOzy Programmer Jul 24, 2000 135 SA Hello How can I convert this string to an array a=1,3,4 such as b[0]=1 b[1]=3 b[2]=4
Aug 2, 2001 #2 rpet Programmer Jun 5, 2000 87 DE You can use strtok() to extract the numbers from the string. char p[] = "1, 3, -6, 0, 129"; int a[5], index = 0; char * token = strtok(p, "," while(token) { a[index++] = atoi(token); token = strtok(0, "," } Upvote 0 Downvote
You can use strtok() to extract the numbers from the string. char p[] = "1, 3, -6, 0, 129"; int a[5], index = 0; char * token = strtok(p, "," while(token) { a[index++] = atoi(token); token = strtok(0, "," }
Aug 2, 2001 #3 Zyrenthian Programmer Mar 30, 2001 1,440 US you could also do this a different way as well int getNextInt(char* array) { int return_value = atoi(array); array = strstr(array,","+1; return return_value; } int main() { ... char* array = a; while(array) { b[index++]=getNextInt(array); } } I suggest this because i have had problems with strtok in the past. strstr has always treated me well Matt Upvote 0 Downvote
you could also do this a different way as well int getNextInt(char* array) { int return_value = atoi(array); array = strstr(array,","+1; return return_value; } int main() { ... char* array = a; while(array) { b[index++]=getNextInt(array); } } I suggest this because i have had problems with strtok in the past. strstr has always treated me well Matt
Aug 2, 2001 #4 Cagliostro Programmer Sep 13, 2000 4,226 GB #inlcude<strstream> using namespace std; int main() { strstream x; char* z; int y=0; x<<y; z=x.str(); x>>y; return 0; } John Fill ivfmd@mail.md Upvote 0 Downvote
#inlcude<strstream> using namespace std; int main() { strstream x; char* z; int y=0; x<<y; z=x.str(); x>>y; return 0; } John Fill ivfmd@mail.md