c++ - Splitting char array -
i have char **names
array stores names file.
this .txt
file
mike, sam, stuart andre, williams, phillips patels, khan, smith
basically, want split , store names before ,
character. example, mike, sam, stuart
become...
newname[0] = mike newname[1] = sam newname[2] = stuart
i have this...
for (int i=0; i<3; i++) { (int j=60, j>0; j--) { if(names[i][j] == ',') { cout << j << endl; //this prints out position. how can store position , something? } } }
i appreciate if me code, in right direction. don't want use vectors
classes
i have attempted store marks of these students, want add double *marks[2]
array.
this .txt
file...
69.9, 56.5 29.8, 20.0 35.6, 45.0
this code...
char **values; char * pch; pch = strtok (values[i], " ,"); while (pch != null) { sscanf(pch, "%f, %f", &marks[i][0], &marks[i][1]); pch = strtok (null, " ,"); }
i getting random values such 1.28277e-307
, 1.96471e+257
look strtok command helpful you.
this code looks hyphen characters , prints stuff... change commas
#include <string.h> #include <stdio.h> int main() { const char str[80] = "this - www.tutorialspoint.com - website"; const char s[2] = "-"; char * newname[100]; /* @ 100 names */ int icurname = 0; char *token; /* first token */ token = strtok(str, s); /* walk through other tokens */ while( token != null ) { printf( " %s\n", token ); newname[icurname] = malloc (char *) (strlen(token) + 1); strcpy(newname[icurname],token); icurrname ++; token = strtok(null, s); } return(0); }
Comments
Post a Comment