Calculate the number of times a character occurs in a string
-
Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer
-
Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer
Made function, no AFAIK. But you can code it quite easy. Take the String as a vector and make a for with an if and a counter. When you find the character... +1, when not... next element.
Greetings. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
-
Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer
Like this: std::string str("this is a test string only"); int num = std::count(str.begin(),str.end(),'a'); Remember: #include #include
-
Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer
Like this: std::string str("this is a test string only"); int num = std::count(str.begin(),str.end(),'a'); Remember to include string and algorithm