爱编程的小笨孩


私信TA

用户名:2119394720

访问量:20440

签 名:

我在成长,总有一天我会足够优秀。

等  级
排  名 163
经  验 6803
参赛次数 6
文章发表 44
年  龄 0
在职情况 学生
学  校 黄河科技学院
专  业 软件工程

  自我简介:

一只想要当凤凰的鸡

TA的其他文章

解题思路:
判断输入的三个字符的大小与判断输入三个数字的大小的方法相同。

首先需要定义三个字符变量数组str1[100],str2[100],str3[100]用来保存三个字符串,然后使用if判断句进行两两比较。


1.先将字符串数组str1[100]和str2[100]进行比较,把较小的字符串保存在数组str1[100]中,较大的保存在str2[100]中;

2.然后再将str1[100]和str3[100]进行比较,把较小的字符串保存在str1[100]中,较大保存在str3[100]中,确保str1[100]中保存的是最小的字符串;

3.最后将str2[100]和str3[100]进行比较,str2[100]中保存较小的,str3[100]保存较大的。


说明:

C语言中字符串的比较规则是 将两个字符串自左至右逐个字符相比(按ASCII码值大小比较),直到出现不同的字符或遇到'\0'为止。


所用到的字符串处理函数:

strcmp函数-----字符串比较函数

           其一般形式为:strcmp(字符串1,字符串2)

          比较结果由函数值带回

        (1)如果字符串1=字符串2,则函数值为0。

        (2)如果字符串1>字符串2,则函数值为一个正整数。

        (3)如果字符串1<字符串2,则函数值为一个负整数。

strcpy函数-----字符串复制函数

           其一般形式为:strcpy(字符串1,字符串2)

           作用是将字符串2复制到字符串1中去。


注意事项:
C语言中,对字符串进行操作时,可以使用C函数库提供的专门对字符串进行处理的函数,若要使用这些处理函数时,应当在程序开头用#include<string.h>把string.h文件包含到本文件中。


参考代码:

#include<stdio.h>
#include<string.h>
int main()
{
    //定义三个字符数组用来保存三个字符串
    char str1[100];
    char str2[100];
    char str3[100];
    //定义字符数组str用来保存临时字符串
    char str[100];
    //从键盘接受三个字符串
    gets(str1);
    gets(str2);
    gets(str3);
    //以下if语句用来判断三个字符串的大小
    if(strcmp(str1,str2)>0)
    {
        strcpy(str,str1);
        strcpy(str1,str2);
        strcpy(str2,str);
    }
    if(strcmp(str1,str3)>0)
    {
        strcpy(str,str1);
        strcpy(str1,str3);
        strcpy(str3,str);
    }
    if(strcmp(str2,str3))
    {
        strcpy(str,str2);
        strcpy(str2,str3);
        strcpy(str3,str);
    }
    //用puts语句输出三个字符串
    puts(str1);
    puts(str2);
    puts(str3);
    return 0;
}


 

0.0分

7 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

照抄冒泡排序
#include <stdio.h>
#include <string.h>

int main()
{ 
	char arr[3][100] = { 0 }, ar[100] = { 0 };
	for (int i = 0; i < 3; i++)
	{
		gets(arr[i]);
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3 - i - 1; j++)
		{
			if (strcmp(arr[j], arr[j + 1]) > 0)
			{
				strcpy(ar, arr[j]);
				strcpy(arr[j], arr[j + 1]);
				strcpy(arr[j + 1], ar);
			}
		}
	}
	for (int i = 0; i < 3; i++)
		puts(arr[i]);
	return 0;
}
2024-03-06 20:32:07
有问题啊,输入
b
a
c
返回
a
c
b
2023-03-02 20:54:40
最后一个写上大于零行忙
2022-10-21 11:29:19
  • «
  • 1
  • »