【发布时间】:2019-01-02 15:29:50
【问题描述】:
我试图将一组 A 和 B 按特定顺序存储到一个字符串中,但不能。
char String[100];
char choc_type;
int F=0,L,i,j,choc_num,N; //F=First, L=Last
scanf("%d",&N);
for(i=0;i<N;i++)
{ //choc_type is 'A' or 'B' and choc_num is no.of A's or B's
scanf("%d %c",&choc_num,&choc_type);
L=F+choc_num;
for(j=F;j<L;j++)
{
if(choc_type=='A')
String[j]='A'; //is this a right way to assign character
else if(choc_type=='B')
String[j]='B';
}
F=L;
L=L+choc_num;
}
printf("%s",String);
getch();
}
【问题讨论】:
-
你能举一个输入和预期输出的例子吗?
-
scanf("%d %c",&choc_num,choc_type);-->scanf("%d %c",&choc_num, &choc_type);. -
输入:1 A、3 B、2 A 输出:ABBBAA
-
将变量命名为“字符串”并不能使其成为一个变量。