我正在尝试获取输入字符串的后缀,但我正在获取前缀,我该如何进行修复?
例如,带有输入字符串“ Hello”的预期结果是:
Hello
ello
ell
el
e
现在正在返回:
Hello
Hell
Hel
He
H
谢谢
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **suffix;
void panic(char *m) {
fprintf(stderr, "%s\n", m);
exit(0);
}
int main(int argc, char *argv[]) {
int n, i;
if (argc != 2)
panic("wrong parameters");
n = strlen(argv[1]);
suffix = (char **)malloc(n * sizeof(char *));
if (suffix == NULL)
error("Memoria no disponible");
for (i = 0; i < n; ++i) {
suffix[i] = (char *)malloc((n + 1 - i) * sizeof(char));
if (suffix[i] == NULL)
error("Memoria no disponible");
sprintf(suffix[i], "%s", argv[1]);
argv[1][n - 1 - i] = '\0';
}
for (i = 0; i < n; i++) {
printf("%d %s\n", i, suffix[i]);
}
return 0;
}
只需替换这两个语句
sprintf(suffix[i], "%s", argv[1]);
argv[1][n - 1 - i] = '\0';
针对此陈述
sprintf(suffix[i], "%s", argv[1] + i );