GESP Level 3 Practice, String Exercise (Knowledge Point 6 in the C++ Level 3 Syllabus), Difficulty ★★☆☆☆.
-
GESP Level 1 Practice Question List
-
GESP Level 1 Real Question List
-
GESP Level 2 Practice Question List
-
GESP Level 2 Real Question List
-
GESP Level 3 Practice Question List
-
GESP Level 3 Real Question List
-
GESP Level 4 Practice Question List
-
GESP Level 4 Real Question List
-
GESP Level 1-5 Syllabus Analysis
luogu-B2122 Word Reversal
Problem Requirements
Problem Description
When writing words, Xiaoming likes to write them backwards. For example,
<span>hello</span>he writes as<span>olleh</span>. Given a sentence written by Xiaoming, please restore all the words.
Input Format
One line, a string representing the sentence, with words separated by spaces.
Output Format
Each word on a new line.
Input Output Example #1
Input #1
olleh dlrow
Output #1
hello
world
Notes/Tips
The length of the sentence does not exceed , only contains lowercase letters and spaces, and there are no extra spaces.
Problem Analysis
Solution Approach
- Read a complete line of input sentence, which contains only lowercase letters and spaces.
- Iterate through each character in the sentence:
- Reverse the current word (reverse the order of letters).
- Output the reversed word.
- Clear the current word to prepare for the next word.
- When encountering a letter, add it to the current word.
- When encountering a space:
- Reverse the last word.
- Output the reversed word.
Complexity Analysis:
- The time complexity is , where n is the total length of the input sentence.
- The space complexity is , where w is the maximum length of a single word.
Example Code
Method 1: Using stringstream and reverse function
#include <algorithm>
#include <iostream>
#include <sstream>
int main() {
// Define a string variable to store the input sentence
std::string str;
// Read a complete line of input
std::getline(std::cin, str);
// Create a string stream to split words
std::stringstream ss(str);
// Define a string variable to store each word
std::string token;
// Use getline to split the string stream by spaces
while (std::getline(ss, token, ' ')) {
// Reverse each word
reverse(token.begin(), token.end());
// Output the reversed word
std::cout << token << std::endl;
}
return 0;
}
Method 2: Manually processing the string
#include <string>
#include <iostream>
int main() {
// Define a string variable to store the input sentence
std::string str;
// Read a complete line of input
std::getline(std::cin, str);
// Define a string variable to store the current word being processed
std::string cur_str;
// Iterate through the input string
for (int i = 0; i < str.length(); i++) {
// If a space is encountered, the current word is complete
if (str[i] == ' ') {
// Output the currently reversed word
std::cout << cur_str << std::endl;
// Clear the current word to prepare for the next
cur_str = "";
} else {
// Add the current character to the front of the word to achieve reversal
cur_str = str[i] + cur_str;
}
}
// Output the last word (since there is no space after the last word)
std::cout << cur_str << std::endl;
return 0;
}
For detailed GESP syllabus, real questions explanations, knowledge expansion, and practice lists, see:
[Top][GESP] C++ Certification Learning Resource Summary
“luogu-” series questions can be evaluated online at Luogu Question Bank.
“bcqm-” series questions can be evaluated online at Programming Enlightenment Question Bank.