《《C Primer Plus》第六 第十一章编程练习答案.docx》由会员分享,可在线阅读,更多相关《《C Primer Plus》第六 第十一章编程练习答案.docx(17页珍藏版)》请在三一办公上搜索。
1、C Primer Plus第六 第十一章编程练习答案1 #include #include #defineSIZE 100 void input(char *, int ); int main(void) chararrSIZE; int n; puts(input the number of n:); scanf(%d, &n); getchar; puts(input your string: ); input(arr, n); printf(%sn, arr); getchar; return 0; void input(char *Arr, intlen) inti; for (i=0
2、; ilen; i+) *(Arr+i)=getchar; *(Arr+i)=0; while(getchar !=n) continue; 2. #include #include #defineSIZE 100 void input(char *, int ); int main(void) chararrSIZE; int n; puts(input the number of n:); scanf(%d, &n); getchar; puts(input your string: ); input(arr, n); puts(arr); getchar; return 0; void
3、input(char *Arr, intlen) inti; for (i=0; ilen; i+) *(Arr+i)=getchar; if (*(Arr+i) = | *(Arr+i)=t break; *(Arr+i)=0; while(getchar !=n) continue; 3. #include #include #defineSIZE 100 | *(Arr+i)= n) void input(char * ); int main(void) chararrSIZE; puts(input your string: ); input(arr); puts(arr); getc
4、har; return 0; void input(char *Arr) charch; inti=1; do ch=getchar; while(ch = | ch =t | ch =n); Arr0=ch; while( (ch=getchar) &( ch!= &ch!=t&ch!=n) Arri=ch; i+; Arri=0; while (getchar!=n) continue; 4. #include #include #defineSIZE 100 void input(char *, int ); int main(void) chararrSIZE; int n; puts
5、(input the number of n:); scanf(%d, &n); puts(input your string: ); input(arr, n); puts(arr); getchar; return 0; void input(char *Arr, intlen) charch; inti=1; do ch=getchar; while(ch = | ch =t | ch =n); Arr0=ch; while( (ch=getchar) &( ch!= &ch!=t&ch!=n) &ilen) Arri=ch; i+; Arri=0; while (getchar!=n)
6、 continue; 5. #include #include #defineSIZE 100 char *find(char *, char ); int main(void) chararrSIZE; charch; char *ps; while(1) puts(input a string:); fgets(arr, 100, stdin); puts(input a character:); ch=getchar; ps=find(arr, ch); if(ps) puts(Find the character!); else puts(Cont find the charcter!
7、); getchar; getchar; return 0; char *find(char *Arr, charc) intlen=strlen(Arr); char *p=Arr; inti; for (i=0; ilen; i+) if (c= *p) return p; p+; if (i=len) returnNULL; return 0; 6. #include #include #defineSIZE 100 intis_within(char *, char ); int main(void) chararrSIZE; charch; int re; while(1) puts
8、(input a string:); fgets(arr, 100, stdin); puts(input a character:); ch=getchar; re=is_within(arr, ch); if(re) puts(Find the character!); else puts(Cont find the charcter!); getchar; getchar; return 0; intis_within(char *Arr, charc) intlen=strlen(Arr); char *p=Arr; inti; for (i=0; ilen; i+) if (c= *
9、p) return 1; p+; if (i=len) return 0; else return -1; 7. #include #include #defineSIZE 20 char *mystrncpy(char *, char *, int ); int main(void) char s1SIZE; char s2SIZE; char *ps; int n; while(1) puts(input string s2:); gets(s2); puts(input string s1:); gets(s1); puts(Input the number of n:); scanf(
10、%d, &n); ps=mystrncpy(s1,s2, n ); puts(After copy:); puts(ps); getchar; getchar; return 0; char *mystrncpy(char *dst, char *src, intlen) inti; char *p1, *p2; int L=strlen(src); p2=src; p1=dst; for (i=0; i(Llen? L:len); i+) *(p1+i)=*(p2+i); return p1; 8. #include #include #defineSIZE 20 char *string_
11、in(char *, char * ); int main(void) char s1SIZE; char s2SIZE; char *ps; while(1) puts(input string s2:); gets(s2); puts(input string s1:); gets(s1); ps=string_in(s1,s2); if (ps) puts(Find it!); else puts(Cont find it!); getchar; getchar; return 0; char *string_in(char *s1, char *s2) int len1, len2;
12、inti, j, temp; char *p1=s1; char *p2=s2; len1=strlen(s1); len2=strlen(s2); if (*p1=0 | *p2 =0) returnNULL; for (i=0; i(len1-len2) returnNULL; 9 #include #include #defineSIZE 20 void fun(char *); int main(void) chararrSIZE; while(1) puts(input a string:); gets(arr); fun(arr); puts(arr); getchar; getc
13、har; return 0; void fun(char *Arr) char *p=Arr; intlen=strlen(Arr); inti; charch; for (i=0; ilen/2; i+) ch=pi; pi=plen-i-1; plen-i-1=ch; 10 #include #include #defineSIZE 20 void fun(char *); int main(void) chararrSIZE; while(1) puts(input a string:); gets(arr); if (arr0=n) break; fun(arr); puts(arr)
14、; getchar; getchar; return 0; void fun(char *Arr) inti, j; char tempSIZE; i=j=0; while(Arri!=0) if (Arri!= ) tempj=Arri; j+; i+; strcpy(Arr, temp); Arrj=0; 11. #include #include #defineSIZE 20 #defineLIM 3 void display(char *String); voidAscii( char *String); void Length(char *String); voidFirstC(ch
15、ar *String); intFirst_word(char String); int main(void) char inputLIMSIZE; charch; char *ptrLIM; while (1) intct=0; puts(please input 10 strings:); while (ctLIM ) gets(inputct); ptrct=inputct; ct+; puts(*); puts(a. print the original strings:); puts(b. print the strings with ASCII order:); puts(c. p
16、rint the strings with length order:); puts(d. print the strings with first character order:); puts(e. quit.); puts(*input your choice *); ch=getchar; switch (ch) casea: display(ptr); break; caseb: Ascii(ptr); break; casec: Length(ptr); break; cased: FirstC(ptr); break; default: return 0; getchar; ge
17、tchar; return 0; void display(char *String) inti; for (i=0; iLIM; i+) puts(Stringi); voidAscii(char *String) char *p; inti,j; for (i=0; iLIM; i+) for (j=0; j0) p=Stringj; Stringj=Stringj+1; Stringj+1=p; display(String); void Length(char *String) char *p; inti,j; for (i=0; iLIM; i+) for (j=0; jstrlen
18、(Stringj+1) p=Stringj; Stringj=Stringj+1; Stringj+1=p; display(String); intFirst_word(charString) inti=0; while (Stringi != ) i+; returni; voidFirstC(char *String) char *p; inti,j; for (i=0; iLIM; i+) for (j=0; jFirst_word(Stringj+1) p=Stringj; Stringj=Stringj+1; Stringj+1=p; display(String); 12 #in
19、clude #include #include int main(void) intch; int word, large, small, punctuation, num, begin; word=large=small=punctuation=num=begin=0; while( (ch=getchar) != EOF) if (isdigit(ch) num+; if (ispunct(ch) punctuation+; if (isupper(ch) large+; if (islower(ch) small+; if ( isalpha(ch) if (begin=0) word+
20、; begin=1; else begin=0; printf(words=%d, large=%d, small=%d, punc=%d, num=%dn, word, large, small, punctuation, num); getchar; return 0; 13. #include #include void Reverse(char *, int ); int main(void) charstr100; intlen; puts(Enter a string:); gets(str); len=strlen(str); Reverse(str, len); getchar
21、; return 0; void Reverse(char *str, intn) inti; charch; for (i=0; in/2; i+) ch=stri; stri=strn-i-1; strn-i-1=ch; for (i=0; in; i+) putchar(stri); putchar(n); 14. #include #include #include int main(intargc, char *argv) double number, value; int p; printf(The command lines has %d arguments:n, argc-1)
22、; number=atoi(argv1); p=atoi(argv2); value=pow(number, p); printf(the result=%lfn, value); getchar; return 0; 15. #include #include #include #include intmyatoi(char *); int main(intargc, char *argv) charstr50=0; int re; while(gets(str) re=myatoi(str); printf(number=%dn, re); getchar; return 0; intmy
23、atoi(char *p) intlen=strlen(p); inti; int n=0; for (i=0; ilen; i+) if (!isdigit(pi) return 0; else n=n*10+(pi-0); return n; 16. #include #include #include #include int main(intargc, char *argv) charch; if (argv10=-) if (argv11=l) while (ch=getchar)!=EOF) putchar(tolower(ch); elseif (argv11=u) while (ch=getchar)!=EOF) putchar(toupper(ch); else while (ch=getchar)!=EOF) putchar(ch); getchar; return 0;