I have a problem with count number of back slash \ in C++, I have this code:
string path = "a\b\c";
int level = 0;
int path_length = path.size();
for(int i = 0; i < path_length; i++){
if(path.at(i) == '\\'){
level++;
}
}
cout << level << endl;
However, the level is always 0! Can you explain why? And how to count the number of /?
Backslashes in your variable should be escaped.
Also you can use
countfunction in algorithms library to avoid looping each character in the string and check whether it is backslash.