用c语言编写程序输入一个字符串,按相反次序输出其中的所有字符
时间: 2024-04-09 17:28:00 浏览: 100
Sure! Here's a C program that takes a string input from the user and outputs all the characters in reverse order:
```c
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int length = strlen(str);
// Reversing the string
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
}
int main() {
char input[100];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
printf("Reversed string: ");
reverseString(input);
return 0;
}
```
In this program, we define a function `reverseString` that takes a character array (`str`) as input and prints each character in reverse order. In the `main` function, we ask the user to enter a string using `fgets` and then call the `reverseString` function passing the user's input.
Please note that this program assumes the input string will be less than or equal to 100 characters in length. If you need to handle longer strings, you can increase the size of the `input` array accordingly.
阅读全文