BUPT-homework/semester1/pset12/1-1-ReverseChar.c
2021-12-05 10:54:29 +08:00

39 lines
757 B
C

#include <stdio.h>
#pragma GCC optimize("Ofast,inline")
//将字符串str中的内容反向打印的函数
void reversePrint(char str[]);
int main() {
char s[100];
scanf("%s", s);
reversePrint(s);
printf("\n");
return 0;
}
/* 请在这里填写答案 */
#include <stdlib.h>
#include <string.h>
void reversePrint(char str[]) {
if (str == NULL) {
return;
}
if (strlen(str) == 1) {
printf("%c", str[0]);
} else {
char *tmp = malloc(sizeof(char) * strlen(str));
/* Why this:
* char *tmp = malloc(sizeof(char) * (strlen(str) - 1));
* doesn't work? */
for (int i = 1, n = strlen(str), j = 0; i < n; i++, j++) {
tmp[j] = str[i];
}
reversePrint(tmp);
free(tmp);
printf("%c", str[0]);
}
}