【发布时间】:2022-01-18 15:52:27
【问题描述】:
我最近在练习循环。我学会了如何打印:例如 home 到 h ho hom home。通过使用
#include <stdio.h>
#include <string.h>
int main (){
char s[100];
printf("Input string = ");
scanf("%[^\n]", s);
for (int i=1; i<=strlen(s); i++){
for(int j = 0; j<i; j++)
printf("%c", s[j]);
printf("\n");
}
return 0;
我怎样才能扭转它,所以它可以
home
hom
ho
h
反而?谢谢。
【问题讨论】:
-
不要使用 scanf:sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html。在这种情况下,你应该只做
char *s = argv[1]。如果你要使用scanf,有很多问题需要注意,但至少要保护自己:if( scanf("%99[^\n]", s) != 1 ){ exit(1);} -
scanf("%[^\n]", s);比gets()差。也不要使用。
标签: c loops for-loop nested-loops c-strings