โจทย์
Count how many characters in text are uppercase English letters A–Z.
Implement count_uppercase(text).
Input
text: The text to inspect.
Output
Return the number of uppercase letters.
เงื่อนไข
0 ≤ length of text ≤ 10,000 (ASCII characters)
ตัวอย่าง
อินพุต:
text = "Hello World"
ผลลัพธ์ที่คาดหวัง:
expected = 2
อินพุต:
std::string text = "Hello World";
ผลลัพธ์ที่คาดหวัง:
long long expected = 2;
อินพุต:
string text = "Hello World";
ผลลัพธ์ที่คาดหวัง:
long expected = 2;
คำอธิบาย
H and W are uppercase: 2.
อินพุต:
text = "no caps here!"
ผลลัพธ์ที่คาดหวัง:
expected = 0
อินพุต:
std::string text = "no caps here!";
ผลลัพธ์ที่คาดหวัง:
long long expected = 0;
อินพุต:
string text = "no caps here!";
ผลลัพธ์ที่คาดหวัง:
long expected = 0;
คำอธิบาย
There are no uppercase letters: 0.
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
สแกนข้อความแค่รอบเดียว แล้วนับเฉพาะตัวอักษร A ถึง Z
คำใบ้ 2
ตรวจสถานะตัวพิมพ์ของแต่ละตัวอักษรโดยตรง แทนการเทียบคำหรือแยกสตริง
คำใบ้ 3
จำไว้ว่าตัวพิมพ์เล็ก ตัวเลข ช่องว่าง และเครื่องหมายวรรคตอนไม่นับ
ความซับซ้อนที่คาดหวัง
เวลา O(n), หน่วยความจำเพิ่ม O(1)
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน