1、编写一个程序,该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目
void statistics()
{
char c;
int i = 0;
int n_space = 0;
int n_lines = 0;
int n_others = 0;
while ((c = getchar()) != '#') {
if (c == ' ')
n_space++;
else if (c == '\n')
n_lines++;
else
n_others++;
}
printf("n_space == %d\n", n_space);
printf("n_lines == %d\n", n_lines);
printf("n_others == %d\n", n_others);
}
2、编写一个程序,该程序输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCll码,每行打印8个字符/编码对。
void ASCllEncodingOfLetters()
{
char ch;
int i = 0;
printf("请输入字符串:");
while ((ch = getchar()) != '#')
{
printf(" %c:%d ", ch, ch);
i++;
if (i % 8 == 0) {
printf("\n");
}
}
}
4、用if else语句编写程序读取的输入,直到#。利用一个感叹号代替每个句号,将原有的每个感叹号用两个感叹号代替,最后输出进行了多少次替换。
void main()
{
char ch;
int n = 0;
printf("请输入字符串:");
while ((ch = getchar()) != '#')
{
if (ch == '.') {
putchar('!');
n++;
}
else if (ch == '!')
{
putchar('!');
putchar('!');
n++;
}
else
putchar(ch);
}
printf("共计进行了%d次替换\n", n);
}