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

16th Dec 2020, 3:45 PM
Hį»Æu Đį»©c Phįŗ”m
Hį»Æu Đį»©c Phįŗ”m - avatar
4 Answers
+ 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
16th Dec 2020, 4:00 PM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 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šŸ•‰)
16th Dec 2020, 10:21 PM
Brian
Brian - avatar
+ 1
Brian, string is not a keyword; it's a valid identifier and thus can be used as variable name.
17th Dec 2020, 3:11 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 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.
17th Dec 2020, 6:34 AM
Brian
Brian - avatar