练习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; }
练习11-2 补
练习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("复制的是:", tmp); scanf("%s", tmp); puts("复制了。"); printf("str = \"%s\"\n", str_copy(str, tmp)); return 0; }
练习11-4
#include <stdio.h> #include<string.h> void put_string(const char *s) { int str_len; int i; str_len = strlen(s); for (i = 0; i < str_len; i++) { printf("%c",*s); s++; } return; } int main(void) { char s[] = "abcdef"; put_string(s); printf("\n"); return 0; }
练习11-5
#include <stdio.h> #include<string.h> int str_chnum(const char *s,int c) { int num = 0; while (*s != '\0') { if (*s == c) num++; s++; } return num; } int main(void) { char s[128]; char c; printf("input string s:"); scanf("%s",s); getchar();//clear buffer printf("input character:"); scanf("%c",&c); printf("string %s has %d character %c\n",s,str_chnum(s,c),c); return 0; }
练习11-6
#include <stdio.h> #include<string.h> char *str_chr(const char *s,int c) { int *chr_flag; while (*s != '\0') { if (*s == c) { chr_flag = s; break; } else chr_flag = NULL; s++; } return chr_flag; } int main(void) { char s[128]; char c; printf("input string s:"); scanf("%s",s); getchar();//clear buffer printf("input character:"); scanf("%c",&c); printf("pointer of character %c is %p\n",c,str_chr(s,c)); return 0; }
练习11-7
#include <stdio.h> #include <ctype.h> void str_toupper(char *s) { while(*s!='\0') { *s = toupper(*s); s++; } return; } void str_tolower(char *s) { while(*s!='\0') { *s = tolower(*s); s++; } return; } int main(void) { int i; char s[128]; printf("input string s:"); scanf("%s",s); printf("output upper string:"); str_toupper(s); printf("%s",s); printf("\n"); printf("output lower string:"); str_tolower(s); printf("%s",s); printf("\n"); return 0; }
练习11-8 暂时有误
练习11-9 先不写
练习11-10 先不写
扫描二维码关注公众号,回复:
1578967 查看本文章