12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main() {
- char *str;
- int length;
- int i;
- str = (char *)malloc(101);
- if (str == NULL) {
- fprintf(stderr, "内存分配失败\n");
- exit(EXIT_FAILURE);
- }
- fgets(str, 101, stdin); //
- if (str[strlen(str) - 1] == '\n') {
- str[strlen(str) - 1] = '\0';
- }
- length = strlen(str);
- char *reversed = (char *)malloc((length + 1) * sizeof(char));
- if (reversed == NULL) {
- fprintf(stderr, "内存分配失败\n");
- exit(EXIT_FAILURE);
- }
- for (i = 0; i < length; i++) {
- reversed[i] = str[length - 1 - i];
- }
- reversed[i + 1] = '\0';
- printf("%s\n", reversed);
-
- free(reversed);
- free(str);
- return 0;
- }
|