程序代码:// 若使用TC/BC/VC6等编译器,把下面代码的scanf_s()/printf_s()函数改成scanf()/printf()即可
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
const int CHARS_NUMBER = 3;
const char END_CHAR = '#';
int main(void)
{
int counter; // 读入字符计数器
char ch[CHARS_NUMBER];
for ( ; ; )
{
printf_s("请输入%d个字符,可用空格或制表符、回车分隔,甚至不分隔,输入%c结束程序:\n", CHARS_NUMBER, END_CHAR);
counter = 0;
while (counter < CHARS_NUMBER)
{
while (scanf_s("%1c", &ch[counter], 1) == 1)
{
if (ch[counter] == END_CHAR)
{
goto end; // 此处因循环嵌套太深用goto跳出,回避不用goto的方法是把本循环封装到函数中
}
if (isalpha(ch[counter]))
{
printf_s("第%d个字符是%c\n", counter + 1, ch[counter]);
++counter;
break;
}
}
}
putchar('\n');
for (counter = 0; counter < CHARS_NUMBER; ++counter)
{
if ((ch[counter] != 'a') && (ch[counter] != 'A'))
{
printf_s("%c左移一位的字符为%c\n", ch[counter], ch[counter] - 1);
}
}
putchar('\n');
}
end:
printf_s("\n按任意键结束程序...");
_getch();
return EXIT_SUCCESS;
}实际上,输入部分会过滤掉所有不符合要求的字符,比如你可以用在中间夹杂数字之类的字符测试,总之,它要读够CHARS_NUMBER个字符为止。另外,你也可以一次性输入3个以上的字符进去看看……
[ 本帖最后由 TonyDeng 于 2014-6-28 14:40 编辑 ]

授人以渔,不授人以鱼。







