0
Do we have any commands in C++ that have the same function like "in" in pascal??
For instance, if I want to find out how many numbers appear in my string, I can write like this in pascal: count:=0; for i:=0 to length(string)-1 do If string[i] in ('0'..'9') then count:=count+1; Please let me know if there is anything in C++ that has the same function as the "in" above
4 Respuestas
+ 3
I answered a similar question 3 days ago:
https://www.sololearn.com/discuss/2624891/?ref=app
btw for your specific case there is a std::isdigit() function in cctype header. It returns nonzero value if argument is numeric character.
inline bool isdigit(char c){
return std::isdigit(c) != 0;
}
int main() {
std::cout << std::boolalpha
<< isdigit('5');
return 0;
}
You can find docs here :
https://en.cppreference.com/w/cpp/string/byte/isdigit
+ 1
There are several ways to do the equivalent in C++. This would be one simple way that resembles what the Pascal example is doing:
count =0;
for (int i=0; i<myString.size(); i++)
if (string("0123456789").find(myString[i]) != string::npos)
count++;
(I used myString because string is a reserved keyword in C++).
EDIT: string is NOT a reserved keyword. It is a pre-defined typedef. (Thank you, 🇮🇳Omkar🕉)
+ 1
Brian, string is not a keyword; it's a valid identifier and thus can be used as variable name.
+ 1
🇮🇳Omkar🕉 thank you for clarifying that string is not a reserved keyword. I learned that it is a pre-defined typedef identifier based on the basic_string class.
Still, I think it is wise to avoid using string as a variable name to avoid confusion.