本文主要是介绍《明解C语言》第三版 (入门篇) 第十一章练习答案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
练习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;
}
//输出“56”,p指向的地址+1后,往后移了一位,读到的内容从“456”变成了“56\0".
练习11-2
#include <stdio.h>#define str_add(arr) ((sizeof(arr)) / (sizeof(arr[0])))
int main(void)
{char s[][5] = { "LISP","C","Ada" };char* p[] = { "PAUL","X","MAC","PC","Linux" };int i;int x = str_add(s);int y = str_add(p);for (i = 0; i < x; i++) {printf("x = \"%s\"\n",s[i]);}for (i = 0; i < y; i++) {printf("x = \"%s\"\n",p[i]);}return 0;
}
练习11-3
#include <stdio.h>/*--- 将字符串s复制到d ---*/
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>void put_string(const char* s)
{putchar(*s);while (*s++){putchar(*s);}
}int main()
{char s[123] ;printf("请输入字符串:");scanf("%s",s);put_string(s);return 0;
}
练习11-5
#include <stdio.h>int str_chnum(const char* s, int c)
{int cnt = 0;while (*s) // while (*s != NULL){if (*s == c){cnt += 1;}*s++;}return cnt;
}int main(void)
{char str[128];char cx;printf("请输入要查找的字符:");scanf("%c", &cx);printf("请输入一个字符串:"); scanf("%s", str);printf("字符串\"%s\"中字符要找的个数为%d\n", str, str_chnum(str, cx));return 0;
}
练习11-6
意思是:输入要找的 a 字符,在字符串 bash 中,输出结果是ash;在bdsadasd中含有多个a
以最先出现的为准,结果是adasd
#include <stdio.h>char* str_chnum(const char* s, int c)
{while (*s++) {char* t = s;if (*s == c) {return t;break;}}return NULL;
}int main() {char s[128];char c;printf("要计数的字符是:");scanf("%c", &c);printf("请输入字符串:");scanf("%s", s);printf("%s", str_chnum(s, c));return 0;
}
练习11-7
#include <ctype.h>
#include <stdio.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()
{char str[128];printf("请输入字符串:");scanf("%s", str);str_toupper(str);printf("大写字母:%s\n", str);str_tolower(str);printf("小写字母:%s\n", str);return 0;
}
练习11-8
#include <stdio.h>void del_digit(char* ptr)
{char temp[128];char* p = temp;while (*ptr != NULL){if (*ptr < '0' || *ptr>'9'){*p = *ptr;p++;}ptr++;}*p = '\0';printf("%s", temp);
}int main()
{char str[128];printf("输入字符串str = ");scanf("%s", str);printf("字符串%s删除数字后为:", str);del_digit(str);return 0;
}
练习11-9
#include <stdio.h>
#include <string.h>void str_renull(char* ptr)
{while (*ptr){*ptr = '\0';ptr++;}
}
int main()
{char str1[128];char str2[128];char temp[128];int n;printf("输入字符串str1 = "); scanf("%s", str1);printf("输入字符串str2 = "); scanf("%s", str2);printf("n = "); scanf("%d", &n);printf("str1长度为%d。\n", strlen(str1));printf("str2长度为%d。\n", strlen(str2));printf("temp复制str1(all) = \"%s\"。\n", strcpy(temp, str1));str_renull(temp);printf("temp复制str2(%d) = \"%s\"。\n", n, strncpy(temp, str2, n));str_renull(temp);strcpy(temp, str1);printf("str1(all) & str2(all) = \"%s\"。\n", strcat(str1, str2));strcpy(str1, temp);printf("str1 & str2(%d) = \"%s\"。\n", n, strncat(str1, str2, n));strcpy(str1, temp);printf("str1(all) - str2(all) = %d。\n", strcmp(str1, str2));printf("str1(%d) - str2(%d) = %d。\n", n, n, strncmp(str1, str2, n));return 0;
}
练习11-10
//编写函数,实现atoi()、atol()、atof()三个函数的功能
#include <stdio.h>
#include <limits.h>int strtoi(const char* nptr)
{int result = 0;int sign = 1;if (*nptr == '-'){sign = -1;nptr++;}if (*nptr == '+'){nptr++;}while (*nptr){result = result * 10 + (*nptr) - '0';nptr++;}return result * sign;
}long strtol(const char* nptr)
{long result = 0;int sign = 1;if (*nptr == '-'){sign = -1;nptr++;}if (*nptr == '+'){nptr++;}while (*nptr){result = result * 10 + (*nptr) - '0';nptr++;}return result * sign;
}double strtof(const char* nptr)
{double result = 0.0;double n = 1.0;int sign = 1;if (*nptr == '-'){sign = -1;nptr++;}if (*nptr == '+'){nptr++;}while (*nptr){if (*nptr == '.'){nptr++;break;}result = result * 10 + (*nptr) - '0';nptr++;}while (*nptr){n /= 10.0;result = result + ((*nptr) - '0') * n;nptr++;}return result * sign;
}int main()
{char str_int[128];char str_long[128];char str_double[128];printf("整形str_int = "); scanf("%s", str_int);printf("长整型str_long = "); scanf("%s", str_long);printf("双精度浮点型str_double = "); scanf("%s", str_double);printf("转换之后\n");printf("\"%s\" => int型 = %d。\n", str_int, strtoi(str_int));printf("\"%s\" => long型 = %ld。\n", str_long, strtol(str_long));printf("\"%s\" => double型 = %.10lf。\n", str_double, strtof(str_double));return 0;
}
这篇关于《明解C语言》第三版 (入门篇) 第十一章练习答案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!