编程笔记
-
使用条件表达式将大写字母转换为小写字母
#include <stdio.h> int lower(int c); int main(void){ char s[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int i = 0; while (s[i] != '/0') { printf("%c -> %c/n", s[i], lower(s…
-
将输入每行一个字词打印
#include <stdio.h> int main(void){ int c = 0, linestarted = 0; printf("Input some characters, then press Ctrl+D./n"); while ((c = getchar()) != EOF) if (c == ' ' || c == '…
-
计算每个数字,空白字符(空白,制表符,换行符)和所有其他字符的出现次数。
#include <stdio.h> int main() { int c, nwhite= 0, nother= 0; int ndigit[10]; for (int i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) { if (c >= '0' && c…
-
将输入复制到其输出,将一个或多个空格的每个字符串替换为一个空格。
#include <stdio.h> int main(void){ int c, blank_recieved = 0; printf("Input some characters, when you finishes, press Ctrl+D./n"); while ((c = getchar()) != EOF){ if (c == ' '…
-
将其输入复制到其输出,用/t替换每个制表符,用/b替换每个退格符,用//替换每个反斜杠。
#include <stdio.h> int main(void){ int c; printf("Input some characters, then press Ctrl+D./n"); while ((c = getchar()) != EOF){ if (c == '/t') printf("//t"); else i…
-
计算输入中的行,单词和字符
#include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ int main() { int c = 0, nl = 0, nw = 0, nc = 0, state = OUT; while ((c = getchar()) != EOF) { ++nc; if (c …
-
计算空格,制表符和换行符的程序
#include <stdio.h> int main(void){ int c, bl, tl, nl; bl = 0; tl = 0; nl = 0; printf("Input some characters, then press Ctrl+D./n"); while ((c = getchar()) != EOF){ if (c == ' …
-
计算用户输入的输入行
#include <stdio.h> int main(){ int c, nl = 0; while ((c = getchar()) != EOF) { if (c == '/n') ++nl; } printf("%d/n", nl); }
-
使用while循环计算输入中的字符数
#include <stdio.h> int main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld/n", nc); return 0; }
-
使用for循环计算输入中的字符数
#include <stdio.h> int main() /*from w w w . ja va 2s. co m*/ { double nc; for (nc = 0; getchar() != EOF; ++nc) ; printf("%.0f/n", nc); }