Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Maybe, the point to watch out here is the last '\0'??
string reverseString(string s) {
if(s.size() <= 1) return s;
int i = 0;
int j = s.size() - 1;
while(i < j) {
swap(s[i], s[j]);
i++;
j--;
}
return s;
}
本文介绍了一个简单的C++函数,该函数接收一个字符串作为输入,并返回其反转后的字符串。通过使用两个指针从两端向中间遍历并交换字符的方式实现。例如,输入hello将返回olleh。
728

被折叠的 条评论
为什么被折叠?



