Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
// This question is for C/C++ programmer.
// #include <algorithm> if programming in C++. otherwise, need to write an extra swap function.
void reverseString(string input) {
// first check the input length.
if(input.size() <= 1) return;
int left = 0, right = input.size() - 1;
while(left < right) {
swap(input[left], input[right]);
left++;
right--;
}
本文将指导您如何使用C++编程语言编写代码来反转C风格字符串,包括检查字符串长度、定义双指针进行交换操作等关键步骤。
248

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



