A - CBC Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
This problem asks to handle strings, especially distinguishing uppercase and lowercase letters.
In case of C++, one can use the isupper
function to check if a letter is an uppercase letter. Many other languages provide similar feature in their standard library.
Alternatively, one can determine it using its character code.
Let ord(x)
be the character code corresponding to a letter \(x\). Then a letter \(x\) is an uppercase English letter if and only if ord('A')
\(\leq \) ord(c)
\(\leq\) ord('Z')
, because uppercase English letters appear consecutively in the ASCII character table. Thus, one can use this condition to determine it. This approach of using character code can also be used in most programming languages.
Sample code 1(C++):
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
if (isupper(c)) cout << e;
}
cout << endl;
}
Sample code 2(C++):
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
if ('A' <= c && c <= 'Z') cout << e;
}
cout << endl;
}
Sample code 1(Python):
s = input()
ans = ""
for c in s:
if c.isupper():
ans += c
print(ans)
Sample code 2(Python):
s = input()
ans = ""
for c in s:
if ord('A') <= ord(c) and ord(c) <= ord('Z'):
ans += c
print(ans)
posted:
last update: