c - Compare position of a string with an integer -
per example:
string: 1 2 3 4 5
i want make loop compare each of string positions int provided user. tried
if(atoi(string[i]) == value)
but didn't seem work, need change other way around? (the int string, strcmp?) in advance
i'm assuming you're doing in c++.
atoi
takes const char*
argument, you're passing char. need this:
std::string str(1, string[i]); if(atoi(str.c_str()) == value) ...
or skip atoi altogether , do
if(string[i]-48 == value) ...
this works because numerals start @ ascii value of 48 , you're checking 1 character @ time.
disclaimer: i'm assuming string
char[] , therefore string[i]
char
Comments
Post a Comment