/* 11.13-1.c -- 从输入读取n个字符(包括空格、制表符和换行符) 把结果存储在一个数组中,这个数组的地址通过参数来传递 */ #include<stdio.h> #define MAX 10 char * getnchar(char * str, int n); int main(void) { char input[MAX]; char * ptr; puts("Input up to 10 characters:/n"); ptr = getnchar(input, MAX-1); if(ptr == NULL) puts("Input error!"); else puts(input); puts("DONE!/n"); return 0; } char * getnchar(char * str, int n) { int i; int ch; for(i=0; i<n; i++) { ch = getchar(); if(ch != EOF) str[i] = ch; else break; } if(ch == EOF) return NULL; else { str[i] = '/0'; return str; } } // 11.13-2.c -- 输入读取n个字符,在n个字符后, // 或第一个空格、制表符、换行符后停止读取输入 // 由上述情况中最先被满足的那个终止读取 #include<stdio.h> #define MAX 20 char * getnchar(char * str, int n); int main(void) { char input[MAX]; char *ptr; puts("Input up to 20 characters:"); ptr = getnchar(input, MAX-1); if(ptr==NULL) puts("Input Error!"); else puts(input); puts("DONE!"); return 0; } char * getnchar(char * str, int n) { char c; int i; for(i=0; i<n; i++) { c = getchar(); if(c!=EOF && c!=' ' && c!='/t' && c!='/n') { str[i] = c; } else { break; } } if(c==EOF) return NULL; else { str[i] = '/0'; return str; } } /* 11.13-5.c -- is_within() 接受两个参数,一个是字符,另一个是字符串指针。 如果字符在字符串中,返回一个非0值(真) 否则,返回0值(假) */ #include<stdio.h> #define LEN 81 int is_within(char letter, char * str); int main(void) { char str[LEN]; char c; int res; puts("Input your string: "); while(gets(str) && str[0]!='/0') { puts("Input your character: "); c = getchar(); while(getchar()!= '/n') continue; res = is_within(c, str); if(res) printf("The character %c is within the string./n/n", c); else printf("The character %c is NOT within the string./n/n", c); puts("Input your string: "); } return 0; } int is_within(char letter, char *str) { while(*str!=letter && str[0]!='/0') str++; return *str; // = 0 if /0 reached, non-zero otherwise } // 11.13-6.c -- 实现 strncpy #include<stdio.h> #define LEN 80 char * my_strncpy(char * des, char * source, int count); int main(void) { char str1[LEN]; char str2[LEN]; char c; int n; printf("Enter your string: "); while(gets(str1) && str1[0]!='/0') { printf("Enter the length of the 2nd string: "); scanf("%d", &n); while(getchar()!= '/n') continue; puts(my_strncpy(str2, str1, n)); printf("Next string: "); } return 0; } char * my_strncpy(char *des, char *source, int count) { char *s1; s1 = des; while(count && (*des++ = *source++)!='/0') { count--; } if(count) while(--count) *des++ = '/0'; return s1; } // 11.13-7.c -- 函数string_in() // 接收两个字符串指针参数。如果第二个字符串被包含在第一个字符串中,返回被包含的字符串开始的地址 // 否则,返回空指针 #include<stdio.h> #include<string.h> #define LEN 80 char *string_in(const char *str1, const char *str2); int main(void) { char s1[LEN]; char s2[LEN]; char * find; printf("Enter String 1: "); while(gets(s1) && (s1[0]!=0)) { printf("Enter String 2: "); gets(s2); find = string_in(s1, s2); if(find) puts(find); else puts("Not Found"); puts("Next string: "); } return 0; } char *string_in(const char *str1, const char *str2) { int l2 = strlen(str2); int tries = strlen(str1) + 1 - l2; // Maximum Number of comparisons int nomatch = 1; if(tries>0) { while((nomatch = strncmp(str1, str2, l2)) && tries--) str1++; } if(nomatch) return NULL; else return (char *) str1; } // 11.13-8.c -- 使输入字符串反序 #include<stdio.h> #include<string.h> #define LEN 80 char * my_strrev(char * target, const char * source); int main(void) { char input[LEN]; char rev[LEN]; puts("Enter a string: "); while(gets(input) && (input[0]!='/0')) { puts("The reverse of the original string: "); puts(my_strrev(rev, input)); puts("Next String: "); } return 0; } char *my_strrev(char * target, const char * source) { char *tmp; int l=strlen(source); tmp = target; tmp[l] = '/0'; while(l>0) { tmp[l-1] = *source++; l--; } return tmp; }