《明解C语言》第三版 (入门篇) 第十一章练习答案

news/2024/7/5 6:32:39

练习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;
}


http://lihuaxi.xjx100.cn/news/1855351.html

相关文章

TA-Lib学习研究笔记(八)——Momentum Indicators 上

TA-Lib学习研究笔记&#xff08;八&#xff09;——Momentum Indicators 上 Momentum Indicators 动量指标&#xff0c;是最重要的股票分析指标&#xff0c;能够通过数据量化分析价格、成交量&#xff0c;预测股票走势和强度&#xff0c;大部分指标都在股票软件中提供。 1. A…

C++ 学习 之 类的初始化与逗号运算符的联动

我们来看一个代码 class A { public:A(int x) {cout << "123" << endl;}A(int x, int y) {cout << "456" << endl;}}a (1, 2); int main() {} 这个代码的输出结果是什么&#xff1f; 答案是 123 因为编译器把 ( 1 , 2 ) 识别…

2分图匹配算法

定义 节点u直接无边&#xff0c;v之间无边&#xff0c;边只存在uv之间。判断方法&#xff1a;BFS染色法&#xff0c;全部染色后&#xff0c;相邻边不同色 无权二部图中的最大匹配 最大匹配即每一个都匹配上min&#xff08;u&#xff0c; v&#xff09;。贪心算法可能导致&…

LLM算法工程师面试题总结

一、请简述对大模型的基本原理和架构的理解。 大型语言模型如GPT&#xff08;Generative Pre-trained Transformer&#xff09;系列是基于自注意力机制的深度学习模型&#xff0c;主要用于处理和生成人类语言。下面简要概述了它们的一些基本原理和架构特点&#xff1a; 基本原…

openEuler学习02-系统基本操作

1、普通用户crontab没权限-以oracle用户为例 orcl:/home/oracledb> crontab -l You (oracle) are not allowed to use this program (crontab) See crontab(1) for more information 处理办法&#xff1a;# echo oracle >> /etc/cron.allow 2、普通用户无su命令的…

使用Libevent创建TCP连接的入门指南

文章目录 介绍安装Libevent创建TCP连接TCP服务器TCP客户端 应用场景 介绍 Libevent是一个用于事件驱动编程的开源库&#xff0c;它提供了跨平台的事件处理和网络编程功能。在本篇博文中&#xff0c;我们将重点介绍如何使用Libevent来创建TCP连接。通过这个简单的入门指南&…

安卓开发学习---kotlin版---笔记(一)

Hello word 前言&#xff1a;上次学习安卓&#xff0c;学了Java开发&#xff0c;简单的搭了几个安卓界面。这次要学习Kotlin语言&#xff0c;然后开发安卓&#xff0c;趁着还年轻&#xff0c;学点新东西&#xff0c;坚持~ 未来的你会感谢现在努力的你~ 主要学习资料&#xff1a…

爬虫从入门到精通(21) |字体加密通杀方案

文章目录 一、了解什么是字体加密二、Python打开字体加密文件三、字体加密的通杀1.静态的字体文件固定顺序的字体2.其他动态变化情况 一、了解什么是字体加密 字体加密是页面和前端字体文件想配合完成的一个反爬策略。通过css对其中一些重要数据进行加密&#xff0c;使我们在代…