c语言字符串作业题,C语言课后习题练习(四)

article/2025/9/14 8:49:52

(ps:提前声明一下:课后习题是备份给自己看的😂)

1.输入3个整数,按由小到大的顺序输出

int main() {

void swap(int *p1, int * p2);

int n1, n2, n3;

int *p1, *p2, *p3;

printf("input three integer n1,n2,n3:");

scanf("%d,%d,%d",&n1,&n2,&n3);

p1 = &n1;

p2 = &n2;

p3 = &n3;

if (n1 > n2)

swap(p1, p2);

if (n1 > n3)

swap(p1, p3);

if (n2 > n3)

swap(p2, p3);

printf("Now,the order is:%d,%d,%d\n",n1,n2,n3);

return 0;

}

void swap(int *p1, int *p2) {

int p;

p = *p1;

*p1 = *p2;

*p2 = p;

}

远行结果:

a408349eefe1

c1.png

2.输入三个字符串,按由小到大的顺序输出

//记得导入头文件:#import

int main() {

void swap(char *, char *);

char str1[20],str2[31],str3[20];

printf("input three line:\n");

gets(str1);

gets(str2);

gets(str3);

if (strcmp(str1, str2) > 0)

swap(str1, str2);

if (strcmp(str1, str3) > 0)

swap(str1, str3);

if (strcmp(str2, str3) > 0)

swap(str2, str3);

printf("Now,the order is:\n");

printf("%s\n%s\n%s\n",str1,str2,str3);

return 0;

}

void swap(char *p1, char *p2){

char p[20];

strcpy(p, p1);

strcpy(p1, p2);

strcpy(p2, p);

}

运行结果:

a408349eefe1

c2.png

(注:在Xcode中运行才显示的warning: this program uses gets(), which is unsafe.关于使用gets()不安全的警告)

3.写一函数,求一个字符串的长度。在main函数中输入字符串,并输入其长度

int main() {

int length(char *p);

int len;

char str[20];

printf("input string: ");

scanf("%s",str);

len = length(str);

printf("The length of string is %d. \n",len);

return 0;

}

int length(char *p) { //求字符串长度函数

int n = 0;

while (*p != '\0') {

n++;

p++;

}

return(n);

}

运行结果:

a408349eefe1

c3.png

4.有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串

//记得导入头文件:#import

int main() {

void copystr(char *, char *, int);

int m;

char str1[20],str2[20];

printf("input string:\n");

gets(str1);

printf("which character that begin to copy?");

scanf("%d",&m);

if (strlen(str1) < m)

printf("input error!");

else {

copystr(str1, str2, m);

printf("result:%s\n",str2);

}

return 0;

}

void copystr(char *p1, char *p2, int m) { //字符串部分复制函数

int n = 0;

while (n < m - 1) {

n++;

p1++;

}

while (*p1 != '\0') {

*p2 = *p1;

p1++;

p2++;

}

*p2 = '\0';

}

运行结果:

a408349eefe1

c4.png

(注:在Xcode中运行才显示的warning: this program uses gets(), which is unsafe.关于使用gets()不安全的警告)

5.输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少

//记得导入头文件:#import

int main() {

int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;

char *p,s[20];

printf("input string: ");

while ((s[i] = getchar()) != '\n')

i++;

p = &s[0];

while (*p != '\n') {

if (('A' <= *p) && (*p <= 'Z'))

++upper;

else if (('a' <= *p) && (*p <= 'z'))

++lower;

else if (*p == ' ')

++space;

else if ((*p <= '9') && (*p >= '0'))

++digit;

else

++other;

p++;

}

printf("upper case:%d lower case:%d",upper,lower);

printf(" space:%d digit:%d other:%d\n",space,digit,other);

return 0;

}

运行结果:

a408349eefe1

c5.png

6.将n个数按输入时顺序的逆序排列,用函数实现

int main() {

void sort(char *p, int m);

int i, n;

char *p, num[20];

printf("input n:");

scanf("%d",&n);

printf("please input these numbers:\n");

for (i = 0; i < n; i++)

scanf("%d",&num[i]);

p = &num[0];

sort(p, n);

printf("Now,the sequence is:\n");

for (i = 0; i < n; i++)

printf("%d",num[i]);

printf("\n");

return 0;

}

void sort(char *p, int m) { //将n个逆序排列函数

int i;

char temp, *p1, *p2;

for (i = 0; i < m/2; i++) {

p1 = p + i;

p2 = p + (m - 1 - i);

temp = *p1;

*p1 = *p2;

*p2 = temp;

}

}

运行结果:

a408349eefe1

c6.png


http://chatgpt.dhexx.cn/article/xyRFnFrg.shtml

相关文章

翁恺C语言课后习题

1 时间换算&#xff08;5分&#xff09; 题目内容&#xff1a; UTC是世界协调时&#xff0c;BJT是北京时间&#xff0c;UTC时间相当于BJT减去8。现在&#xff0c;你的程序要读入一个整数&#xff0c;表示BJT的时和分。整数的个位和十位表示分&#xff0c;百位和千位表示小时…

啊哈C语言课后练习题

第 四 章 第四节 项目一&#xff1a;求1~100所有偶数的和。 问题代码/*copyright (啊哈C语言), 学习C *All rights reserved. *文件名称:myfirstc. *作者&#xff1a;JohnLu *完成日期&#xff1a;2020年3月17日 *版本号V1.0 * *问题描述&#xff1a;求1~100所有偶数的和。 …

12道c语言的课后习题!

1、计算n的阶乘&#xff08;1*2*3*4*5 n是个数&#xff0c;比如说乘到100&#xff1f;&#xff09; // 计算n的阶乘#include"stdio.h"int main() {int n 0;scanf("%d", &n);int i 0;int ret 1;for (i 1; i < n; i) {ret * i;}printf("…

TeeChart在VS2005,VS2008的入门教程

最近项目上要用到TeeChart来绘图&#xff0c;网络上关于TeeChart的资料很多&#xff0c;但是感觉很多都讲不到点上&#xff0c;很多入门过程中碰到的细节问题还是要自己探索。我把这两天的学到的记下来&#xff0c;也与大家分享。 环境&#xff1a;VS2005,VS2008 TeeChart版本:…

怎么用VS2008?

请问各位怎么用VS2008&#xff0c;为什么总是出错&#xff1f;我是这么做的。

[Win8]如何使用Visual Studio2012进行Windows8项目开发

随着Windows8普通版&#xff0c;专业版和企业版的普及&#xff0c;Windows8的应用开发也逐渐火热起来。 下面简单介绍一下如何使用Visual Studio2012进行Windows8项目的开发。 首先安装Windows8的操作系统&#xff0c;推荐安装32位的Win8&#xff0c;因为64位的容易出现不兼容…

VS2008技巧收集

1&#xff0c;Visual Studio 2008自带的1000多个 Windows 系统使用的各种图标、光标和动画文件在Visual Studio 2008的安装目录下&#xff0c;/Microsoft Visual Studio 9.0/Common7/VS2008ImageLibrary/2052文件夹下面&#xff0c;有一个VS2008ImageLibrary.zip&#xff0c;这…

VS2012使用 MSDN教程

VS2012安装默认只安装Help Viewer,如果想要安装文档的话&#xff0c;需要去官网下载。网址如右:https://www.microsoft.com/zh-CN/download/details.aspx?id34794 一&#xff0c;选择中文版&#xff0c;点击下载&#xff0c;2G。如下图&#xff1a; 下载完成&#xff0c;需要解…

VS2008使用技巧及快捷键大全

VS2008技巧&#xff0c;非常实用&#xff0c;非常提高效率。 1&#xff0c;Visual Studio 2008自带的1000多个 Windows 系统使用的各种图标、光标和动画文件 在Visual Studio 2008的安装目录下&#xff0c; \Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\2052文件夹…

VS2008 使用小技巧-------快捷键

以下的内容转载自&#xff1a;http://www.21industry.com/group_thread/view/id-32435 VS2008 使用小技巧——快捷键 1. 怎样调整代码排版的格式&#xff1f; 选择&#xff1a;编辑—>高级—>设置文档的格式 或 编辑—>高级—>设置选中代码的格式。 格式化cs代码…

vs2008 html5 的安装,vs2008安装教程,详细教您vs2008安装教程

小伙伴们&#xff0c;你们知道Visual Studio 2008是什么吗&#xff1f;小编我来给你们简单的解释一下哈&#xff0c;vs2008就是是对Office 2007、Web 2.0的下一代开发工具&#xff0c;代号是Orcas&#xff0c;是对vs2005的升级。而今天我就来告诉你们vs2008安装教程&#xff0c…

【C++】利用Visual Studio 2008编写C++,Visual Studio 2008的基本使用

现在很多高校还是利用Visual C 6.0这一经典版本在教C&#xff0c;很多人对自己第一个程序的回忆是利用Visual C 6.0用C语言写的Helloworld&#xff0c;之后再学习C的类&#xff0c;不过还是在使用Visual C 6.0。于是Visual Studio对Visual C 6.0界面的布置改动得比较大&#xf…

VS2008使用技巧

VS2008技巧&#xff0c;非常实用&#xff0c;非常提高效率。 1&#xff0c;Visual Studio 2008自带的1000多个 Windows 系统使用的各种图标、光标和动画文件 在Visual Studio 2008的安装目录下&#xff0c; \Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\2052文件夹…

vs2008/vs2010新手快速入门必读教程

1.新建一个项目并且添加项 ①打开文件->新建->项目&#xff0c;新建一个项目 ②选择win32控制台&#xff0c;然后点击下一步&#xff0c;出现对话框&#xff0c;在空项目前面打勾&#xff0c;单击完成。 ③出现一个空白的页面&#xff0c;在左侧的一栏空白处单击右键&…

vs2008怎么创建c语言程序,VS2008的使用

使用之前我们先准备一段代码。#include #include int main() { printf("欢迎进入www.dotcpp.com编程网站&#xff01;"); system("pause"); return 0; } 1. VS2008的使用过程 1) 打开软件&#xff0c;第一次打开需要耗费一点时间。 2) 创建文件的方式为点…

vs2008使用技巧推荐

VS2008技巧&#xff0c;非常实用&#xff0c;非常提高效率。 1&#xff0c;Visual Studio 2008自带的1000多个 Windows 系统使用的各种图标、光标和动画文件 在Visual Studio 2008的安装目录下&#xff0c; \Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\2052文件夹…

Microsoft Visual Studio 2008安装教程

Microsoft Visual Studio 2008安装包链接&#xff1a;https://pan.baidu.com/s/1nwRnWj3 密码&#xff1a;s00k①解压安装包后得到后缀为.iso的镜像文件&#xff0c;解压&#xff1b; ②解压后找到安装启动程序setup.exe&#xff0c;双击&#xff1b; ③双击后进入安装界面&…

VS2008 入门基本操作

VS2008 入门操作 更新日期&#xff1a;2011-7-31 测试环境&#xff1a;VS2008WinXP ━━━━━━━━━━━━━━━━━━━━━━━━ 这段时间正式开始使用VS2008。从VC6过度到VS2008还真的有点不习惯。 下面列出一些常用操作&#xff0c;希望有所帮助。 以下只是个入门的…

【转】VS2008 入门基本操作

VS2008 入门操作 更新日期&#xff1a;2011-7-31 测试环境&#xff1a;VS2008WinXP ━━━━━━━━━━━━━━━━━━━━━━━━ 这段时间正式开始使用VS2008。从VC6过度到VS2008还真的有点不习惯。 下面列出一些常用操作&#xff0c;希望有所帮助。 以下只是个入门的…

android 多线程断点续传下载 三

今天跟大家一起分享下android开发中比较难的一个环节,可能很多人看到这个标题就会感觉头很大,的确如果没有良好的编码能力和逻辑思维,这块是很难搞明白的,前面2次总结中已经为大家分享过有关技术的一些基本要领,我们先一起简单回顾下它的基本原理。 http://blog.csdn.net/shim…