int main(void){ ..._明解c语言入门篇答案第十一章">
赞
踩
1、练习11-1
#include<stdio.h>
int main(void)
{
char *p="123";
printf("p=\"%s\"\n",p);
p="456"+1;
printf("p=\"%s\"\n",p);
return 0;
}
2、练习11-2
#include<stdio.h> int main(void) { int i, n1, n2; char a[][5]={"LISP","C","Ada"}; char *p[]={"Paul","X","MAC"}; n1 = sizeof(a) / sizeof(a[0]); n2 = sizeof(p) / sizeof(p[0]); for(i=0;i<n1;i++){ printf("a[%d]=\"%s\"\n",i,a[i]); } for(i=0;i<n2;i++){ printf("a[%d]=\"%s\"\n",i,p[i]); } return 0; }
3、练习11-3
#include<stdio.h> char* str_copy(char *d, const char *s){ char *t=d; while(*d++=*s++); return t; } int main(void) { char str[128]="ABC"; char tmp[128]; printf("str=\"%s\"\n",str); printf("复制的是:\n",tmp); printf("复制了"); scanf("%s",tmp); printf("str=\"%s\"\n",str_copy(str,tmp)); return 0; }
4、练习11-4
#include<stdio.h>
void put_string(const char *s){
while(*s){
printf("%c",*s++);
}
}
int main(void)
{
char *str="ABC";
put_string(str);
return 0;
}
5、练习11-5
#include<stdio.h> int str_chnum(const char *s,int c){ int num=0; while(*s){ if(*s==c){ num++; } *s++; } return num; } int main(void) { char *str="ABccc"; char ch='c'; printf("字符C的个数为%d\n",str_chnum(str,ch)); return 0; }
6、练习11-6
#include<stdio.h> char *str_chr(const char *s,int c){ char *p; while(*s){ if(*s==c){ p=s; break; } else{ p=NULL; } *s++; } return p; } int main(void) { char *str="ABccc"; char ch='c'; printf("出现第一个字符C的指针为%d\n",*str_chr(str,ch)); return 0; }
7、练习11-7
#include<stdio.h> #include<ctype.h> void str_toupper(char *s){ while(*s){ *s=toupper(*s); *s++; } } void str_tolower(char *s){ while(*s){ *s=tolower(*s); *s++; } } int main(void) { char str[128]; printf("请输入字符串:"); scanf("%s",str); str_toupper(str); printf("大写字母:%s\n",str); str_tolower(str); printf("小写字母:%s\n",str); return 0; }
8、练习11-8
#include<stdio.h> #include<string.h> void del_digit(char *d,char *s){ while(*s){ if((*s>='a'&&*s<='z')||(*s>='A'&&*s<='Z')){ *d=*s; *d++; } *s++; } } int main(void) { char des[128]; char src[128]; printf("请输入字符串:"); scanf("%s",src); del_digit(des,src); printf("删除字符串内的所有数字字符:%s\n",des); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。