c++ reverse
时间: 2023-10-18 21:29:47 浏览: 90
reverse c++
Sure! If you want to reverse a string in C, you can use the following code:
```c
#include <stdio.h>
#include <string.h>
void reverseString(char* str) {
int length = strlen(str);
int start = 0;
int end = length - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
char str[] = "Hello, World!";
printf("Original string: %s\n", str);
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
```
This code defines a function `reverseString` that takes a character array (`str`) as input and reverses the string in-place. The `main` function demonstrates how to use this function by reversing the string "Hello, World!" and printing the result.
阅读全文