赞
踩
练习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
- #include <stdio.h>
-
-
- int main(void)
- {
- int i, j;
- char a[][5] = { "ABCD","E","EGH" };
- char *p[] = { "PAUL","X","MAC" };
-
- i = sizeof(a) / sizeof(a[0]);
- j = sizeof(p) / sizeof(p[0]);
-
- printf("a中字符串个数为%d\n", i);
- printf("b中字符串个数为%d\n", j);
-
- return 0;
-
- }
练习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("复制的是:");
- scanf_s("%s", tmp, 32);
-
- 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 len = 0;
- for (len = 0; len < strlen(s); len++)
- printf("%c", s[len]);
- }
-
-
- int main(void)
- {
- char str[128] ;
- printf("请输入字符串:");
- scanf_s("%s", str, 32);
-
- printf("str=\"");
- put_string(str);
- puts("\"");
- return 0;
- }
练习11-5
- #include <stdio.h>
- #include <string.h>
-
- int str_chnum(const char *s, int c)
- {
- int len, num = 0;
-
- for (len = 0; len < strlen(s); len++) {
- if (*(s + len) == 'c')
- num++;
- }
- return num;
- }
-
- int main(void)
- {
- char str[128];
- char c = 'c';
-
- printf("请输入字符串:");
- scanf_s("%s", str, 32);
-
- printf("字符串中有%d个c。\n", str_chnum(str, c));
- return 0;
- }
练习11-6
- #include <stdio.h>
- #include <string.h>
-
- char* str_chr( char *s, int c)
- {
- char *t = s;
- while (*s) {
- if (*s == c) {
- t = s;
- break;
- }
- else t = NULL;
- s++;
- }
- return t;
- }
-
-
- int main(void)
- {
- char str[128];
- int c = 'c';
-
- printf("请输入字符串:");
- scanf_s("%s", str, 32);
-
- printf("目标地址为%p\n", str_chr(str, c));
-
- return 0;
- }
练习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 s[128];
- printf("请输入字符串:");
- scanf("%s", s,32);
-
- str_toupper(s);
- printf("将字符串中的字母改为大写:%s\n", s);
- str_tolower(s);
- printf("将字符串中的字母改为小写:%s\n", s);
- return 0;
- }
练习11-8
- #include <stdio.h>
-
- void del_digit(char *str)
- {
- while (*str) {
- if ((*str >= 'a'&&*str <= 'z') || (*str >= 'A'&&*str <= 'Z')) {
- printf("%c", *str);
- }
- str++;
- }
- }
-
- int main(void)
- {
- char str[128]="12AbC123DeF";
-
- printf("去掉数字后,字符串变为:");
- del_digit(str);
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。