我正在尝试编写一个程序,该程序从用户读取字符串并以对角线模式打印字符串的字符。我将字符串的最大长度设置为50个字符。该程序可以以对角线模式打印字符,但它不能正确打印字符。
#include<stdio.h>
int main () {
int i = 0, j = 0, m;
char c[50];
printf("Enter a string: ");
scanf("%c", c);
m = sizeof(c) / sizeof(c[0]);
for (i = 1; i <= m; i++) {
for (j = 1; j <= i; j++) {
if (j == i) {
printf("%c", c[i-1]);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
答案 0 :(得分:1)
检查以下代码:
#include<stdio.h>
int main ()
{
int i=0,s=0;
char c[50];
printf("Enter a string: ");
scanf("%s",c);
while(c[i] != '\0')
{
s = i;
while(s--)
printf(" ");
printf("%c\n",c[i]);
i++;
}
return 0;
}