系列: Algorithms
cpp
42 行
· 更新於 2026-02-03
reverse_string.cpp
Algorithms/reverse_string.cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string line;
// read input line by line until EOF
while (getline(cin, line)) {
int length = line.length();
// reverse the line manually
for (int i = 0; i < length / 2; i++ ) {
char left = line[i];
char right = line[length - i - 1];
// left - convert to opposite case
if (islower(left)) {
left = toupper(left);
} else if (isupper(left)) {
left = tolower(left);
}
// right - convert to opposite case
if (islower(right)) {
right = toupper(right);
} else if (isupper(right)) {
right = tolower(right);
}
line[i] = right;
line[length - i - 1] = left;
}
// print the reversed line
cout << line << endl;
}
return 0;
}
相關文章
Algorithms
java
更新於 2026-03-02
#include <iostream>.java
#include <iostream>.java — java source code from the Algorithms learning materials (Algorithms/#include <iostream>.java).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
748.cpp
748.cpp — cpp source code from the Algorithms learning materials (Algorithms/748.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
827.cpp
827.cpp — cpp source code from the Algorithms learning materials (Algorithms/827.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
827_best_greedy.cpp
827_best_greedy.cpp — cpp source code from the Algorithms learning materials (Algorithms/827_best_greedy.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
8402.cpp
8402.cpp — cpp source code from the Algorithms learning materials (Algorithms/8402.cpp).
閱讀文章 →
Algorithms
cpp
更新於 2026-04-07
860.cpp
860.cpp — cpp source code from the Algorithms learning materials (Algorithms/860.cpp).
閱讀文章 →