c primer plus 11章习题答案

来源:互联网 发布:网络教育文凭国家承认吗 编辑:程序博客网 时间:2024/05/17 06:10
//输入n个字符,包括换行符,把结果存储在一个数组当中#include <stdio.h>void input (char *p,int n);int main (void){char a[30];int n;puts("input the char number of your string");scanf("%d",&n);getchar();puts("input your string:");input(a,n);puts(a);return 0;}void input (char *p,int n){int i;for(i=0;i<n;i++)*(p+i)=getchar();*(p+i)='0';}
//第二题#include <stdio.h>#include <ctype.h>void input (char *p,int n);int main (void){char a[30];int n;puts("input the char number of your string");scanf("%d",&n);getchar();puts("input your string:");input(a,n);puts(a);return 0;}void input (char *p,int n){int i;for(i=0;i<n;i++){*(p+i)=getchar();if(isspace(*(p+i)))break;}*(p+i)='0';}
//第三题#include <stdio.h>#include <ctype.h>void word(char *p);int main (void){char a[30];puts("please input your string");gets(a);word(a);puts(a);return 0;}void word(char *p){int begin,end;for(begin=0;isspace(*(p+begin));begin++)continue;for(end=begin;!isspace(*(p+end));end++)continue;*(p+end)='\0';for(;*(p+begin)!='\0';p++)*p=*(p+begin);*p='\0';}
//4#include <stdio.h>char *mystrchr(char *,char);int main (void){char str[81];  char ch;char *p;do{puts("input a string");gets(str);puts("input match char");ch=getchar();getchar(); //清除换行p=mystrchr(str,ch);if(p){puts("find!");puts(p);}else puts("can't find!");puts("input any char expect q to go on.");gets(str);}while(*str!='q');puts("quit!");return 0;}char *mystrchr(char *p,char ch){char *p_save=p;if(*p=='\0')return NULL;while (1){if(*p==ch){return p_save;}else {if(*++p=='\0')return NULL;p_save=p;}}}

//5int is_within(char ,char *);#include <stdio.h>int main (void){char str[81];char ch;int n;do{puts("input a string");gets(str);puts("input match char");ch=getchar();getchar();n=is_within(ch,str);if(n==0)puts("can't find");elseputs("find!");puts("input any char expect q to go on");gets(str);}while (*str!='q');puts("bye");return 0;}int is_within(char ch,char *p){if(*p=='\0')return 0;while(1){if(*p==ch)return 1;else{if(*++p=='\0')return 0;}}}

//6#include <stdio.h>char *strncpy(char *s1,char *s2,int n);int main (void){char s1[81],s2[81];int n;do{puts("input string s1");gets(s1);puts("input string s2");gets(s2);puts("input a limit number");scanf("%d",&n);getchar();strncpy(s1,s2,n);puts("after copy");puts(s1);puts("input any char expect q to go on");gets(s1);}while (*s1!='q');puts("bye!");return 0;}char *strncpy(char *s1,char *s2,int n){int i;for(i=0;i<n-1;i++,s1++,s2++)*s1=*s2;*s1='\0';}

#include <stdio.h>char *string_in (char *,char *);int main (void){char s1[81],s2[81];do{puts("input string 1");gets(s1);puts("input string 2");gets(s2);puts("after operation");puts(string_in(s1,s2));puts("input any char expect q to go on");gets(s1);}while(*s1!='\0');return 0;}char *string_in (char *p1,char *p2){char *p_save1=p1,*p_save2=p2;if(*p1=='\0'||*p2=='\0')return NULL;while(1){if(*p1==*p2){if(*++p2=='\0')return p_save1;if(*++p1=='\0')return NULL;}else{if(*++p1=='\0')return NULL;p_save1=p1;p_save2=p2;}}}

#include <stdio.h>void *reverse(char *p);int main (void){char str[81];do{puts("input a string");gets(str);puts("reverse");reverse(str);puts(str);puts("input any char expect q to go on");gets(str);}while(*str!='q');return 0;}void *reverse(char *p){int i,n;char temp;for(n=0;*(p+n)!='\0';n++)continue;n--;for(i=0;i<n-i;i++){temp=p[i];p[i]=p[n-i];p[n-i]=temp;}}


#include <stdio.h>void delete_space(char *p1);int main (void){char s1[81];do{puts("input a string");gets(s1);puts("after delete_space");delete_space(s1);puts(s1);puts("input any char expect q to go on");gets(s1);}while(*s1!='q');return 0;}void delete_space(char *p1){char *p2;while(*p1!='\0'){if(*p1==' '){p2=p1;while(*p2!='\0'){*p2=*(p2+1);p2++;}p1--;}p1++;}}

#include <stdio.h>#include <string.h>void initial(const char **,int n); //输出初始字符串列表void ascll(char **,int n);//ASCLL顺序输出字符串void length(char **,int n);//长度递增顺序输出字符串void limit(char **,int n);//按字符串中第一个单词的长度输出字符串#define SIZE 81#define LIM 10int main (void){char str[LIM][SIZE]; //存放10个字符串char *pr[LIM]; //指向字符串的指针int i; //用于计数char ch;//用于菜单的选择/*读入10个字符串,直到遇到EOF*/puts("输入 10 个字符串,EOF 退出");for(i=0;i<LIM&&gets(str[i])!=NULL;i++)pr[i]=str[i];//提供菜单选择puts("menu");printf("a:输入初始字符串列表      b:按ASCII顺序输出字符串\n""c:按长度递增顺序输出字符串  d:按字符中的第一个单词输出字符串\n""e:退出\n");scanf("%c",&ch);getchar();//清空缓存区/*菜单循环*/while(ch!='e'){switch(ch){case 'a':initial(pr,LIM); //输出字符串列表break;case 'b': ascll(pr,LIM);//ASCLL顺序输出字符串break;case 'c':length(pr,LIM);//长度递增顺序输出字符串break;case 'd':limit(pr,LIM);//按字符串中第一个单词的长度输出字符串break;        default:puts("请输入a-e");break;}puts("再次选择菜单");scanf("%c",&ch);getchar();//清空缓存区}puts("BYE!");return 0;}void initial(const char **string,int n) //输出初始字符串列表{int i;for(i=0;i<n;i++)puts(string[i]);}void ascll(char **string,int n)//ASCLL顺序输出字符串{//复制一份原字符串,在备份进行处理char copy[LIM][SIZE];int i;char temp[SIZE];int top,seek;for(i=0;i<n;i++)strcpy(copy[i],string[i]);//使用冒泡法比较大小for(top=0;top<n-1;top++)for(seek=top+1;seek<n;seek++)if(strcmp(copy[top],copy[seek])>0){strcpy(temp,copy[top]);strcpy(copy[top],copy[seek]);strcpy(copy[seek],temp);}for(i=0;i<n;i++)puts(copy[i]);}void length(char **string,int n)//长度递增顺序输出字符串{//复制一份原字符串,在备份进行处理char copy[LIM][SIZE];int i;char temp[SIZE];int top,seek;for(i=0;i<n;i++)strcpy(copy[i],string[i]);//使用冒泡法比较大小for(top=0;top<n-1;top++)for(seek=top+1;seek<n;seek++)if(strlen(copy[top])>strlen(copy[seek])){strcpy(temp,copy[top]);strcpy(copy[top],copy[seek]);strcpy(copy[seek],temp);}for(i=0;i<n;i++)puts(copy[i]);}void limit(char **string,int n)//按字符串中第一个单词的长度输出字符串{//复制一份原字符串,在备份进行处理char copy[LIM][SIZE];int i;unsigned int size;//size=strlen(string[0])char temp[SIZE];int top,seek;for(i=0;i<n;i++)strcpy(copy[i],string[i]);size=strlen(string[0]);for(i=0;i<n;i++)if(strlen(copy[i])>size)*(copy[i]+size)='\0';for(i=0;i<n;i++)puts(copy[i]);}

#include <stdio.h>#include <ctype.h>int main (void){int word=0,upper=0,lower=0,punct=0,digit=0,begin=0;char ch;while((ch=getchar())!=EOF){if(isalpha(ch)){if(begin==0){word++;begin=1;}if(isupper(ch)) upper++;if(islower(ch)) lower++;if(ispunct(ch)) punct++;if(isdigit(ch)) digit++;}else begin=0;}printf("word %d\nupper %d,lower %d,punct %d\n digit %d",word,lower,punct,digit);return 0;}

<pre name="code" class="html">//不用命令行的方式
#include <stdio.h>
#include <ctype.h>#include <string.h>int main (void){char str[81];unsigned int i,j,temp;char *p;int word=0;gets(str);p=str; //先将整个字符串进行交换for(i=0;i<strlen(str)/2;i++){temp=p[i];p[i]=p[strlen(str)-1-i];p[strlen(str)-1-i]=temp;}puts(str);//再将每个字符串的字母进行交换for(i=0;i<strlen(str);i++,p++){if(isalpha(*p)){word++;}elseif(word>1){for(j=0;j<word/2;j++){temp=*(p-1-j);*(p-1-j)=*(p-word+j);*(p-word+j)=temp;}word=0;}}puts(str);return 0;}
#include <stdio.h>int main (int argc,char *argv[]){<span style="white-space:pre"></span>int i;<span style="white-space:pre"></span>for(i=argc;i>1;i--)<span style="white-space:pre"></span>printf("%s ",argv[i-1]);<span style="white-space:pre"></span>return 0;}
#include <stdio.h>#include <stdlib.h>#include <math.h>int main (int argc,char *argv[]){<span style="white-space:pre"></span>double a;<span style="white-space:pre"></span>int b;<span style="white-space:pre"></span>double c;<span style="white-space:pre"></span>if(argc!=3)<span style="white-space:pre"></span>printf("erro!please print a double and a integer\n");<span style="white-space:pre"></span>else <span style="white-space:pre"></span>{<span style="white-space:pre"></span>a=atof(argv[1]);<span style="white-space:pre"></span>b=atoi(argv[2]);<span style="white-space:pre"></span>c=pow(a,b);<span style="white-space:pre"></span>printf("%.2lf ^ %d =%lf\n",a,b,c);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return 0;}
//使用字符分类函数实现atoi函数#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>int main (void){<span style="white-space:pre"></span>int a,begin=0;<span style="white-space:pre"></span>unsigned int i;<span style="white-space:pre"></span>char str[81];<span style="white-space:pre"></span>gets(str);<span style="white-space:pre"></span>a=atoi(str);<span style="white-space:pre"></span>printf("%d\n",a);<span style="white-space:pre"></span>/*使用字符分类的方法*/<span style="white-space:pre"></span>for(i=0;i<strlen(str);i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(isdigit(str[i])==0&&begin==0)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>printf("0\n");<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else if(isdigit(str[i]))<span style="white-space:pre"></span>{<span style="white-space:pre"></span>printf("%c",str[i]);<span style="white-space:pre"></span>begin=1;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>putchar('\n');<span style="white-space:pre"></span>return 0;}
#include <stdio.h>#include <ctype.h>int main (int argc,char *argv[]){<span style="white-space:pre"></span>int ch;<span style="white-space:pre"></span>int ok=1;<span style="white-space:pre"></span>char mode='p';<span style="white-space:pre"></span>if(argc>2)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>puts("erro,please input -p,-u or -l");<span style="white-space:pre"></span>ok=0;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else if(argc==2)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(argv[1][0]!='-')<span style="white-space:pre"></span>{<span style="white-space:pre"></span>puts("erro !");<span style="white-space:pre"></span>ok=0;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>switch(argv[1][1])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>case 'p':<span style="white-space:pre"></span>case 'u':<span style="white-space:pre"></span>case 'l':mode=argv[1][1];<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>default:printf("%s is an invalid flag",argv[1]);<span style="white-space:pre"></span>printf("using -p");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if(ok)<span style="white-space:pre"></span>while((ch=getchar())!=EOF)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>switch(ch)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>case 'p':putchar(ch);<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case 'u':putchar(toupper(ch));<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case 'l':putchar(tolower(ch));<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span>return 0;}
0 0