0
我必须添加两位数的字符串,意思是1234 12 + 34(至少这是我收集的)。我写了一个程序,这样做只会有一个例外,那就是当最后一个数字没有配对时它不会正确添加。两位数的字符串加在末尾没有数字
下面是代码我有:
void main()
{
char string[1000];
int count,sum=0,x,y;
printf("Enter the string containing both digits and alphabet\n");
scanf("%s",string);
for(count=0;count < string[count]; count++)
{
x=(string[count] - '0') * 10;
y=(string[count+1] - '0') + x;
sum += y;
count++;
}
printf("Sum of string in two digit array is =%d\n",sum);
}
所以基本上,如果我有123的程序执行12+(30-48),而不是12 + 3。我已经坐了一段时间,无法弄清楚如何解决这个问题,任何提示或建议将受到欢迎。
(如1234或4567的字符串将做12 + 34及45 + 67)
'计数<串[COUNT]'?? – BLUEPIXY
输入“12”会怎样?或'1234567'? – lared
count