NOIP 2007年信息学奥赛初赛普及组阅读程序写结果:
#include "ctype.h" #include "stdio.h" void expand(char s1[], char s2[]) { int i, j, a, b, c; j = 0; for (i = 0; (c = s1[i]) != '\0'; i++) if (c == '-') { a = s1[i - 1]; b = s1[i + 1]; if (isalpha(a) && isalpha(b) || isdigit(a) && isdigit(b)) /*函数isalpha(a)用于判断字符a是否为字母,isdigit(b) 用于判断字符b是否为数字,如果是,返回1,否则返回0 */ { j--; do s2[j++] = a++; while (tolower(a) < tolower(s1[i + 1])); } /*函数tolower(a)的功能是当字符a是大写字母,改为小写,其余情况不变*/ else s2[j++] = c; } else s2[j++] = c; s2[j] = '\0'; } main() { char s1[100], s2[300]; printf("input s1:"); gets(s1); expand(s1, s2); printf("%s\n", s2); }
输入:wer2345d-h454-82qqq
输出: