2-B【函数 atoi 字符串->整型数】
1 2 3 4 5 6 7 8 9 | 1-B【文件复制】 1-C【字符统计】 1-D【行计数】 1-E【单词计数】 1-F【统计各个数字、空白符以及所有其它字符出现的次数】 2-B【函数 atoi 字符串->整型数】 2-E【字符串拼接函数 strcat】 3-A【折半查找 binsearch】 3-C【atoi 字符串->整型数】 |
该函数的测试程序可以写成:
1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> int main(int argc, char *argv[]) { int c; /* freopen("1-B.in.txt","r",stdin); */ while ((c = getchar()) != EOF) putchar(c); return 0; } |
你知道应该把 atoi 这个函数放在什么位置吗?
不知道? 问问同学吧!
2-C【lower 转换成小写】
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #include <ctype.h> int main(int argc, char *argv[]) { int c; /* freopen("1-B.in.txt","r",stdin); */ while ((c = getchar()) != EOF) putchar(tolower(c)); return 0; } |
2-D【函数squeeze 从字符串中删除指定字符】
1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> int main(int argc, char *argv[]) { int nc = 0; while (getchar() != EOF) ++nc; printf("%d\n", nc); return 0; } |
可以考虑将从字符串 s 删除指定字符c后的字符保存到字符串 t 中,然后将 t 复制到 s 中,这样做或许更容易理解。
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main(int argc, char *argv[]) { int c, nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl); return 0; } |
事实上,聪明的你或许会发现字符串 t 是多余的,直接保存在 s 中是完全可以的,这样就有了先前的程序。
测试程序如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ int main(int argc, char *argv[]) { int c, nw = 0, state = OUT; while ((c = getchar()) != EOF) { if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d\n", nw); return 0; } |