Count the letters#8571

Count the letters (Editorial)

The task is to count how many times the letter appears in the string . Uppercase and lowercase letters are considered identical.

Algorithm implementation

Read the input string and the character .

getline(cin, s);
cin >> ch;

Count the occurrences of the letter in the string . Convert each character to lowercase and compare it with , which is given in lowercase.

cnt = 0;
for (i = 0; i < s.length(); i++)
  if (tolower(s[i]) == ch) cnt++;

Print the answer.

cout << cnt;

Python implementation

Read the input string and the character .

s = input()
ch = input()

Convert the string to lowercase. This ensures that the character count is case-insensitive, meaning 'A' and 'a' are treated as the same character. Then, store in the variable the number of occurrences of the character in the string after it is converted to lowercase.

res = s.lower().count(ch)

Print the answer.

print(res)