C基础4.8作业

1.循环输入5个人姓名 (二维字符数组),计算最大值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int i;
	char str[5][10]={};
	for(i=0;i<5;i++)
		gets(str[i]);	
	char max[10]="";
	strcpy(max,str[0]);
	for(i=0;i<5;i++)
	{
		if(strcmp(max,str[i])<0)
	strcpy(max,str[i]);
	}
	printf("max  = %s\n ",max);
	return 0;
}

2.在主函数中定义字符串数组并初始化,自定有参有返函数,

计算字符串长度,并返回长度

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int strlenth(char str[]);
int main(int argc, const char *argv[])
{
	char str[20]="";
	gets(str);
	int lenth = strlenth(str);
	printf("strlenth = %d\n",strlenth(str));
	printf("strlenth = %d\n",lenth);
	return 0;
}
int strlenth(char str[])
{
	int i=0;
	while(str[i]!='\0')
		i++;
	return i;
}

3.在主函数中定义两个字符串并初始化,定义有参无返函数,实现字符串连接

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void strlink(char a[],char b[]);
int main(int argc, const char *argv[])
{
	char a[20]="";
	char b[20]="";
	printf("a:");
	gets(a);
	printf("b:");
	gets(b);
	strlink(a,b);
	printf("a + b :%s\n",a);
	return 0;
}
void strlink(char a[],char b[])
{
	int i=0;
	while(a[i]!='\0')
		i++;
	for(int j=0;j<strlen(b);j++,i++)
		a[i]=b[j];
}

4.在主函数中定义两个字符串并初始化,定义有参有返函数,实现字符串比较,返回两个字符串ASCII值的差。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int strcompare(char a[],char b[]);
int main(int argc, const char *argv[])
{
	char a[20]="";
	char b[20]="";
	printf("a:");
	gets(a);
	printf("b:");
	gets(b);
	printf("a - b = %d\n",strcompare(a,b));
	return 0;
}
int strcompare(char a[],char b[])
{
	int i=0,sub;
	while(a[i]==b[i])
	{
		if(a[i]=='\0')
			break;
		i++;
	}
	sub=a[i]-b[i];
	if(sub>0)
		puts("a>b");
	if(sub==0)
		puts("a=b");
	if(sub<0)
		puts("a<b");
	return sub;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值