Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

How do you count the occurrence of a given character in a string? c++

#include <iostream>
#include <algorithm>

int main(){
  std::string str;
  std::getline(std::cin, str);
  for 
    (char target: 
      {'a','b','c','d','e','f','g','h','i','j','k','l',
      'm','n','o','p','q','r','s','t','u','v','w','x','y','z'})
  {
    size_t num_target = std::count(str.begin(), str.end(), target);
    if
      (num_target>=1)
      std::cout << target <<" : " << num_target << '
';
  }
  return 0;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #count #occurrence #character
ADD COMMENT
Topic
Name
1+3 =