กำหนด string นับจำนวน: ตัวพิมพ์ใหญ่ (A-Z), ตัวพิมพ์เล็ก (a-z), ตัวเลข (0-9), อักขระพิเศษ
ตัวอย่าง: "Tutori@lPo!n&90" → Upper: 2, Lower: 8, Digit: 2, Special: 3
วิธีแก้ปัญหา
- วนลูปทุกตัวอักษร
- ตรวจสอบช่วง ASCII: 'A'-'Z', 'a'-'z', '0'-'9', ที่เหลือ = special
ตัวอย่างโค้ด C++
#include <bits/stdc++.h>
using namespace std;
void countChars(string str) {
int upper=0, lower=0, digit=0, special=0;
for(char c : str) {
if(c >= 'A' && c <= 'Z') upper++;
else if(c >= 'a' && c <= 'z') lower++;
else if(c >= '0' && c <= '9') digit++;
else special++;
}
cout << "Upper: " << upper << "\nLower: " << lower
<< "\nDigit: " << digit << "\nSpecial: " << special;
}
int main() {
countChars("Tutori@lPo!n&90");
}
Input
"Tutori@lPo!n&90"
Output
Upper: 2, Lower: 8, Digit: 2, Special: 3