/* 11.13-9.c -- 删除函数字符串中的空格 */ #include<stdio.h> #define LEN 80 void delsp(char *string); int main(void) { char str[LEN]; puts("Enter a string:"); while(gets(str) && (str[0]!='/0')) { delsp(str); puts(str); puts("Next String:"); } return 0; } void delsp(char *string) { char * tmp; tmp = string; while(*string) { if(*string != ' ') { *tmp++ = *string++; } else string++; } *tmp = '/0'; } /* 读取10个字符串或遇到EOF,停止读取 为用户提供一个有5个选项的菜单 1. 输出初始字符串列表 2. 按ASCII顺序输出字符串 3. 按长度递增顺序输出字符串 4. 按字符串中第一个单词的长度输出字符串 5. 退出 */ // UNAVAILABLE // 11.13-11.c -- 读取输入,直到遇到EOF // 并报告单词数、大写字母数、小写字母数、标点符号数和数字符号数 #include<stdio.h> #include<ctype.h> #include<stdbool.h> #define LEN 81 int main(void) { int c; int num_word=0; int num_upper=0; int num_lower=0; int num_punct=0; int num_digit=0; bool inword = false; // == ture if c is in a word while((c=getchar())!=EOF) { if(islower(c)) num_lower++; else if(isupper(c)) num_upper++; else if(ispunct(c)) num_punct++; else if(isdigit(c)) num_digit++; if(!isspace(c) && !inword && isalpha(c)) { inword = true; // starting a new word num_word++; } if(isspace(c) && inword) inword = false; // reached end of a word } printf("/nwords = %d, lowercase = %d, uppercase = %d, digits = %d, punctuation = %d/n", num_word, num_lower, num_upper, num_digit, num_punct); return 0; } //11.13-12.c -- 按照相反的单词顺序显示命令行参数 #include<stdio.h> int main(int argc, char *argv[]) { int i; for(i=argc-1; i>0; i--) printf("%s ", argv[i]); return 0; } // 11.13-13.c -- 计算乘幂的基于命令行的程序 // 第一个命令行参数为double类型,作为幂的底数 // 第二个参数为整数,作为幂的指数 #include<stdio.h> #include<stdlib.h> #include<math.h> int main(int argc, char *argv[]) { int i; double mult; double base; int index; if(argc!=3) printf("Usage: %s number exponent/n", argv[0]); else { base = atof(argv[1]); index = atoi(argv[2]); printf("TOTAL: %g", pow(base, index)); } return 0; } // 11.13-14.c -- 使用字符分类函数atoi()函数 #include <stdio.h> #include <ctype.h> int Atoi(const char *); int main(void) { char ar1[80]; scanf("%s", ar1); printf("%d", Atoi(ar1)); return 0; } int Atoi(const char *str) { int result = 0; int signal = 1; // 默认为正数 if((*str>='0'&&*str<='9') || *str=='+' || *str=='-') // 判断第一个字符是否合法 { //如果是-,符号变为-1,指针右移 //如果是+,指针右移(总之指向了第一个数字) if(*str=='-' || *str=='+') { if(*str == '-') signal = -1; str++; } } else return 0; while(*str>='0'&&*str<='9') result = result * 10 + (*str++ - '0'); return result*signal; } // 11.13-15.c -- 读取输入,直到遇到文件结尾,并把文件显示出来 // 程序可以识别并执行下面的命令行参数 // -p 按照原样显示输入 // -u 把输入全部转换为大写 // -l 把输入全部转换为小写 #include<stdio.h> #include<ctype.h> #define LEN 80 int main(int argc, char *argv[]) { char mode = 'p'; int ok = 1; int ch; if(argc > 2) { printf("Usage: %s [-p -u -l]/n", argv[0]); ok = 0; // skipping processing input } else if(argc == 2) { if(argv[1][0] != '-') { printf("Usage: %s [-p -u -l]/n", argv[0]); ok = 0; } else switch(argv[1][1]) { case 'p': case 'u': case 'l': mode = argv[1][1]; break; default : printf("%c is an invalid flag; ", argv[1]); printf("Using default flag (-p)./n"); } } if(ok) { while((ch=getchar()) != EOF) { switch(mode) { case 'p': putchar(ch); break; case 'u': putchar(toupper(ch)); break; case 'l': putchar(tolower(ch)); } } } return 0; }