Editorial for Chuyển đổi xâu
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Spoiler Alert
Hint 1
Duyệt qua mảng kí tự, nếu phần tử hiện
Reference AC code | O(n) time | O(n) auxiliary space | String
C++
int main()
{
string s;
getline(cin, s);
int n = s.size();
for (int i = 0; i < n; ++i)
{
char c = s[i];
if ('a' <= c && c <= 'z') { cout << char(c - 32); continue; }
if ('A' <= c && c <= 'Z') { cout << char(c + 32); continue; }
cout << c;
}
return 0;
}
Reference AC code | O(n) time | O(1) auxiliary space | String, Online Solving
C++
inline bool isValid(char c) { return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == ' '); }
int main()
{
for (char c; isValid(c = getchar()); cout << char(c == ' ' ? c : c ^ 32));
return 0;
}
Comments