I am trying to split specific string with couple of different ways. The example of my input is (-5,3,0,1,-2)
.
And this is my first code,
// code 1
string s = "(-5,3,0,1,-2)";
int j = 0;
int * temp = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
if (s[i] != '(' && s[i] != ',' && s[i] != ')') {
temp[j++] = (s[i]-'0');
}
}
code 1 works well except, it converts -
sign to ascii value(45) not negative int value.
//code2
char *first = _strdup(s.c_str());
char * temp2 = NULL;
char *temp = strtok_s(first, "(,)", &temp2);
/* Expected output is
temp[0] = -5
temp[1] = 3
temp[2] = 0
temp[3] = 1
temp[4] = -2
*/
However middle of debugging, temp
contains ascii value, not string value. Also not sure code2
is correctly working.
Thanks in advances!