Cho chuỗi và số nguyên k. Đếm số chữ cái (A-Z, a-z) có giá trị ASCII nhỏ hơn k và lớn hơn hoặc bằng k.
Bảng ASCII: A-Z (65-90), a-z (97-122).
Ví dụ: "TuTorials PoinT", k=100 → Nhỏ hơn: 6, Lớn hơn/bằng: 9.
Giải thuật: Duyệt chuỗi, đếm ký tự str[i] < k, phần còn lại là ≥ k.
Ví dụ
#include <bits/stdc++.h>
using namespace std;
int Less_than(string str, int k, int length) {
int count = 0;
for (int i = 0; i < length; i++)
if (str[i] < k) count++;
return count;
}
int main() {
string str = "TuTorials PoinT";
int k = 100;
int len = str.length();
cout << "Less than k: " << Less_than(str, k, len);
cout << "\nGreater or equal k: " << len - Less_than(str, k, len);
return 0;
}
Đầu ra
Count of alphabets having ASCII value less than k are: 6 Count of alphabets having ASCII value equals or greater than k are: 9