练习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语言 入门篇 第十一章答案的更多相关文章

  1. 明解C语言 入门篇 第五章答案

    练习5-1 /* 依次把1.2.3.4.5 赋值给数组的每个元素并显示(使用for语句) */ #include <stdio.h> int main(void) { int i; ]; ...

  2. 明解C语言 入门篇 第四章答案

    练习4-1 #include <stdio.h> int main(void) { int no; int x; do{ printf("请输入一个整数:"); sca ...

  3. 明解C语言 入门篇 第三章答案

    练习3-1 #include <stdio.h> int main() { int x; int y; puts("请输入两个整数."); printf("整 ...

  4. 明解C语言 入门篇 第十三章答案

    练习13-1 /* 打开与关闭文件 */ #include <stdio.h> int main(void) { ]; FILE* fp; printf("请输入你要打开的文件& ...

  5. 明解C语言 入门篇 第七章答案

    练习7-1 #include <stdio.h> int main() { int n; printf(,, ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4 ...

  6. 明解C语言 入门篇 第六章答案

    练习6-1 /* 求两个整数中的最小值 */ #include <stdio.h> /*--- 返回三个整数中的最小值 ---*/ int min2(int a, int b) { int ...

  7. 明解C语言 中级篇 第四章答案

    练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...

  8. 明解C语言 中级篇 第三章答案

    练习3-1 /* 猜拳游戏(其四:分割函数/显示成绩)*/ #include <time.h> #include <stdio.h> #include <stdlib.h ...

  9. 明解C语言 入门篇 第二章答案

    练习2-1 #include <stdio.h> int main() { int x; int y; int percent; puts("请输入两个整数"); pr ...

随机推荐

  1. win7系统下安装Ubuntu18.04组成双系统

    最近在闲鱼上花了350大洋淘到了一台tinkpad sl510,这大概是一台发布于2009年的一台电脑了吧,处理器是酷睿二t440,2Gddr3的显卡,让我有点意外的是这台电脑的硬盘是7200转的32 ...

  2. Java开发桌面程序学习(一)——JavaFx+Jfoenix初始以及搭建

    Java开发桌面程序学习(一)--JavaFx+Jfoenix初始以及搭建 前言 想做一个Java的桌面程序,但是,使用原生的Swing感觉又十分麻烦,那个布局都是拿代码设置,看着十分的乱,偶然的情况 ...

  3. 算法基础:BFS和DFS的直观解释

    算法基础:BFS和DFS的直观解释 https://cuijiahua.com/blog/2018/01/alogrithm_10.html 一.前言 我们首次接触 BFS 和 DFS 时,应该是在数 ...

  4. Element-ui中为上传组件添加表单校验

    vue所依赖的Element的UI库在使用其中的upload组件时,可能很大几率会遇到这个题,需要给upload组件添加表单校验 大家这里直接看代码就可以 <el-form-item class ...

  5. 告诉你一些DBA求职面试技巧

    告诉你一些DBA求职面试技巧 要自信!永远不要低估你的能力.如果你不了解什么问题的答案,承认它.重点放在你找出答案的能力和你学习的意愿. 不要自大!是的,你可能过于自信而被认为是骄傲的.轻率的,甚至是 ...

  6. protocol buffers 使用方法

    protocol buffers 使用方法 为什么使用 Protocol Buffers 我们接下来要使用的例子是一个非常简单的"地址簿"应用程序,它能从文件中读取联系人详细信息. ...

  7. 渗透测试学习 十八、 XSS跨站脚本漏洞详解 续

    XSS平台搭建 ISS+zkeysphp 首先在IIS中新建一个网站,详细过程可以看我之前写搭环境的文章 (下面的写入选项不需要选中) 安装ZKEYS PHP 修改数据库配置 用户名:root 密码: ...

  8. CodeForces - 1245D(思维+最小生成树)

    题意 https://vjudge.net/problem/CodeForces-1245D 已知一个平面上有 n 个城市,需要个 n 个城市均通上电 一个城市有电,必须在这个城市有发电站或者和一个有 ...

  9. 数据分析三剑客 numpy,oandas,matplotlib

    数据分析: 是不把隐藏在看似杂乱无章的数据域背后的信息提炼出来,总结出所研究对象内在规律 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩 ...

  10. **目录找出最后一次修改的文件(html结果),发送报告到指定qq邮箱

    import unittest,HTMLTestRunnerimport osdef runa(): path=os.getcwd() print(path) a=unittest.defaultTe ...