编写一个C程序,该程序包括两个函数: reverseString 和main。reverseString函数接受一个 字符数组(字符串)作为参数,并就地反转该字符串。 在main函数中,调用 reverseString 函数并打印反 转后的字符串。
时间: 2025-06-09 19:01:37 浏览: 12
当然,我可以帮你编写一个简单的C程序,它包含`reverseString`和`main`两个函数。这里是一个例子:
```c
#include <stdio.h>
#include <string.h>
// Function to reverse a string in-place
void reverseString(char str[], int len) {
int start = 0;
int end = len - 1;
while (start < end) {
// Swap characters at the start and end indices
char temp = str[start];
str[start] = str[end];
str[end] = temp;
// Move towards center
start++;
end--;
}
}
int main() {
char input[100]; // Define a buffer for user input
printf("Enter a string: ");
fgets(input, sizeof(input), stdin); // Read input from user, excluding newline
// Ensure null terminator is added if not present
size_t len = strlen(input);
if (input[len - 1] != '\n') {
input[len] = '\0';
}
// Call reverseString function
reverseString(input, len);
// Print the reversed string
printf("Reversed string: %s\n", input);
return 0;
}
```
在这个程序中,`reverseString`函数通过双指针法实现了字符串的反转,而`main`函数则负责获取用户输入、调用`reverseString`以及显示结果。当运行这个程序时,它会提示用户输入一个字符串,然后展示其反转后的版本。
阅读全文
相关推荐















