How to write code to reverse a string in the C language?
Here is an example code in C language to reverse a string.
#include <stdio.h>
#include <string.h>
void reverseString(char* str) {
int length = strlen(str);
int i, j;
char temp;
for (i = 0, j = length - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main() {
char str[100];
printf("请输入一个字符串: ");
gets(str);
reverseString(str);
printf("反转后的字符串: %s\n", str);
return 0;
}
In the code above, we first define a reverseString function that takes a string parameter, str, and reverses the string by swapping its characters. In the main function, we input a string, call the reverseString function to reverse the string, and output the result.