明解C语言 入门篇 第十一章答案
练习11-1
/*
用指针实现的字符串的改写
*/ #include <stdio.h> int main(void)
{
char* p = ""; printf("p = \"%s\"\n", p); p = ""+; /* OK! */ printf("p = \"%s\"\n", p); return ;
}
只能输出“56”,因为p指向的地址+1后,整体往后移了一位,所以读到的内容从“456”变成了“56\0".
练习11-2
/*
字符串数组
*/ #include <stdio.h> int main(void)
{
int i;
char a[][] = { "LISP", "C", "Ada" };
char* p[] = { "PAUL", "X", "MAC","SKTNB"};//用 sizeof(a) / sizeof(a[0])表示数组元素个数
for (i = ; i <( sizeof(a) / sizeof(a[]));i++)
printf("a[%d] = \"%s\"\n", i, a[i]); for (i = ; i < (sizeof(p) / sizeof(p[])); i++)
printf("p[%d] = \"%s\"\n", i, p[i]); return ;
}
练习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[] = "ABC";
char tmp[]; printf("str = \"%s\"\n", str); printf("复制的是:", tmp);
scanf("%s", tmp); puts("复制了。");
printf("str = \"%s\"\n", str_copy(str, tmp)); return ;
}
练习11-4
#include <stdio.h>
void put_string(const char* s) {
putchar(*s);
while (*s++)
{
putchar(*s);
}
}
int main() {
char s[] ;
printf("请输入字符串:");
scanf("%s",s);
put_string(s);
}
练习11-5
#include <stdio.h>
int str_chnum(const char* s,int c) {
int cnt = ;
while (*s != NULL) {
if (*s == c) {
cnt++;
}
*s++;
}
return cnt;
}
int main() {
char s[] ;
char c ;
printf("要计数的字符是:");
scanf("%c", &c);
printf("请输入字符串:");
scanf("%s",s);
printf("%d", str_chnum(s, c));
}
练习11-6
#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[] ;
char c ;
printf("要计数的字符是:");
scanf("%c", &c);
printf("请输入字符串:");
scanf("%s",s);
printf("%s", str_chnum(s, c));
}
练习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(void)
{
char str[]; printf("请输入字符串:");
scanf("%s", str); str_toupper(str);
printf("大写字母:%s\n", str); str_tolower(str);
printf("小写字母:%s\n", str); return ;
}
练习11-8
#include <stdio.h>
#include<stdlib.h> int strtoi( const char* nptr )
{
int sign = , num = ;
if (*nptr == '-') {
sign = -;
nptr++;
}
while (*nptr)
{
num = num * + (*nptr - '');
nptr++;
}
return num * sign;
} int main() {
char c[];
printf("请输入字符串:");
scanf("%s", c);
int m = strtoi(c);
printf("%d",m);
}
明解C语言 入门篇 第十一章答案的更多相关文章
- 明解C语言 入门篇 第五章答案
练习5-1 /* 依次把1.2.3.4.5 赋值给数组的每个元素并显示(使用for语句) */ #include <stdio.h> int main(void) { int i; ]; ...
- 明解C语言 入门篇 第四章答案
练习4-1 #include <stdio.h> int main(void) { int no; int x; do{ printf("请输入一个整数:"); sca ...
- 明解C语言 入门篇 第三章答案
练习3-1 #include <stdio.h> int main() { int x; int y; puts("请输入两个整数."); printf("整 ...
- 明解C语言 入门篇 第十三章答案
练习13-1 /* 打开与关闭文件 */ #include <stdio.h> int main(void) { ]; FILE* fp; printf("请输入你要打开的文件& ...
- 明解C语言 入门篇 第七章答案
练习7-1 #include <stdio.h> int main() { int n; printf(,, ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4 ...
- 明解C语言 入门篇 第六章答案
练习6-1 /* 求两个整数中的最小值 */ #include <stdio.h> /*--- 返回三个整数中的最小值 ---*/ int min2(int a, int b) { int ...
- 明解C语言 中级篇 第四章答案
练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...
- 明解C语言 中级篇 第三章答案
练习3-1 /* 猜拳游戏(其四:分割函数/显示成绩)*/ #include <time.h> #include <stdio.h> #include <stdlib.h ...
- 明解C语言 入门篇 第二章答案
练习2-1 #include <stdio.h> int main() { int x; int y; int percent; puts("请输入两个整数"); pr ...
随机推荐
- Java入门系列之字符串特性(二)
前言 上一节我们讲解到字符串本质上就是字符数组,同时详细讲解了字符串判断相等需要注意的地方,本节我们来深入探讨字符串特性,下面我们一起来看看. 不可变性 我们依然借助初始化字符串的方式来探讨字符串的不 ...
- 简单解决 VMWare “无法打开内核设备:\\Global\\vmx86”错误
在“服务”后右击选择使用管理员打开.然后在一大串服务中找到vm开头的服务项,全部都启动.重新启动vm就ok了(vm需要以管理员身份打开).
- PHP中设计模式以及魔术方法
1.设计模式 1.1单例模式 口诀:三私一公 1.私有的静态属性用来保存对象的单例 2.私有的构造方法用来阻止在类的外部实例化 3.私有的__clone阻止在类的外部clo ...
- XAF-内置初始化数据 (XPO)
Open the Updater.cs (Updater.vb) file, located in the MySolution.Module project's Database Update fo ...
- 3.智能快递柜(通信篇-HTTP)
1.智能快递柜(开篇) 2.智能快递柜(终端篇) 3.智能快递柜(通信篇-HTTP) 4.智能快递柜(通信篇-SOCKET) 5.智能快递柜(通信篇-Server程序) 6.智能快递柜(平台篇) 7. ...
- WindowServer优化
Windows Server 2016 禁止自动更新 1. 打开cmd,输入sconfig,出现如下图: 2. 输入5回车,在输入m回车,完成关闭自动更新.
- [转]Oracle 11g R2 RAC高可用连接特性 – SCAN详解
原文地址:http://czmmiao.iteye.com/blog/2124373 昨天帮朋友解决11g RAC SCAN问题,当时为这朋友简单解答了一些SCAN特性相关的问题,但我知道这仅仅是 ...
- Shell—三剑客(grep、sed、awk)
grep命令详解 文本搜索工具,根据用户指定的“模式(pattern)”对目标文本进行过滤,显示被模式匹配到的行. 命令格式:grep [options] pattern filename.gr ...
- Maven配置以及环境搭配
1. Maven简单介绍 Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念. 作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部署 ...
- word最近文档清除