#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
int s_num = 0;
int e_num = 0;
int o_num = 0;
while ((ch = getchar()) != '#')
{
if(isspace(ch))
s_num++;
else if (ch =='\n')
e_num++;
else
o_num++;
}
printf("空格数为%d, 换行数为%d, 所有其他字符的数为%d.\n",
s_num, e_num, o_num);
return 0;
}
2
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
int num = 0;
while ((ch = getchar()) != '#')
{
printf("%c-%d ", ch, ch);
num++;
if (num % 8 == 0)
printf("\n");
}
return 0;
}
3
#include<stdio.h>
int main(void)
{
int a;
int all_num = 0;
int o_num = 0;
int j_num = 0;
double o_avg = 0.0;
double j_avg = 0.0;
while (scanf("%d", &a) == 1 && a != 0)
{
all_num++;
if (all_num % 2 == 0)
{
o_avg += (double)a;
o_num++;
}
else
{
j_avg += (double)a;
j_num++;
}
}
o_avg /= o_num;
j_avg /= j_num;
printf("输入的偶数的个数为%d,", o_num);
if (o_num > 0)
printf(" 这些偶数的平均值为%.1lf.\n", o_avg);
printf("输入的奇数的个数为%d,", j_num);
if (j_num > 0)
printf("这些奇数的平均值为%.1lf.\n", j_avg)
return 0;
}
4
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
int i = 0;
int j = 0;
printf("Please enter something to test this program (# to quit): ");
while ((ch = getchar()) != '#')
{
if ('.' == ch)
{
putchar('!');
i++;
}
else if ('!' == ch)
{
putchar('!');
putchar('!');
j++;
}
else
putchar(ch);
}
printf("\n");
printf("%d %s.\n", i + j, i + j == 1? "time" : "times");
return 0;
}
5
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
int i = 0;
int j = 0;
printf("Please enter something to test this program (# to quit): ");
while ((ch = getchar()) != '#')
{
switch(ch)
{
case '.': putchar('!');
i++;
break;
case '!': putchar('!');
putchar('!');
j++;
break;
default: putchar(ch);
}
}
printf("\n");
printf("%d %s.\n", i + j, i + j == 1 ? "time" : "times");
return 0;
}
6
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
char prev;
int i = 0;
printf("Please enter something to test this program (# to quit): ");
while ((ch = getchar()) != '#')
{
if ('e' == ch)
prev = ch;
if ('i' == ch && 'e' == prev)
i++;
}
printf("%d %s.\n", i, i == 1 ? "time" : "times");
return 0;
}