#include <stdio.h>
#include <conio.h>
void main()
{
int a = 0, b = 0, c = 0, e[10000];
char d[10000];
printf("Enter text: ");
gets(d);
while(d[a] != '\0')
{
if(d[a] != ' ')
{
b++;
}
else
{
e[c]=a;
c++;
}
a++;
}
printf("Number of characters without spaces: %d\n", b);
for(int i=0; i<=c; i++)
printf("Spaces : %d \n", e[i] );
getch();
}
每次我运行程序时,如预期的那样,空格的输出结果是空格的位置,最后是空格:0。如果我从 i<=c to i
首先这段代码无法编译,因为你有太多的右括号。
其次,您尝试直接打印数组 e,这导致打印内存地址而不是实际元素。您应该遍历数组并逐一打印元素:
for (int i = 0; i < c; i++) {
printf("%d ", e[i]);
}