C语言问题比较三个字符串的大小,由小到大输出。
更新日期:2024.06.02
/*
可以不使用C标准库提供的strcmp函数,但是两个字符串是要比较的
不比较是不能知道谁小谁大的.(设想,如果不比较,当然你可以眼看出来,但是计算机是不知 道的,计算机只根据我们给予了它的指令才执行的)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int str_compare(const char s[],const char n[])// 自定义比较函数
{
// 原理上与C语言标准库中的strcmp功能是一样的.
register int i;
for(i=0;true;i++)
{
if(s[i]=='\0'||n[i]=='\0')
{
if(s[0]>n[0]) return 1;
else if(s[0]<n[0]) return -1;
else return 0;
}
if(s[i]==n[i]) continue;
else{
if(s[0]>n[0]) return 1;
else if(s[0]<n[0]) return -1;
}
}
return 0;
}
int bubble_sort(char **p_str,const int n) // 第一个形参是二级指针
{
int i,swaped,index=0;
char **temp=NULL,**temp2=NULL; // 定义临时二级指针变量用于元素的比较
char *j=NULL; // 定义一级指针用于元素交换.
if(p_str==NULL) return -1; // 杜绝野指针存在
do
{
swaped=0;
temp=temp2=p_str;
for (i=1;i<n;i++)
{
temp=p_str+i;
temp2=p_str+(i-1);
if((str_compare(*temp,*temp2))==-1)
{
j=*temp;
*temp=*temp2;
*temp2=j;
swaped=1;
}
}
} while (swaped);
return 1;
}
int main()
{
char *str[]={"China","India","America","England"};
char **p_str;
int i;
printf(" 比较前:");
for (i=0;i<4;i++){
p_str=str+i;
printf("%s ",*p_str);
}
printf("
");
printf("由小至大:");
p_str=str+0;
if(bubble_sort(p_str,4)==1){
for (i=0;i<4;i++){
p_str=str+i;
printf("%s ",*p_str);
}
}
else{printf("发生了不可预料的错误!");}
printf("
");
return 1;
}
越悦璐3525 :答:/* 可以不使用C标准库提供的strcmp函数,但是两个字符串是要比较的 不比较是不能知道谁小谁大的.(设想,如果不比较,当然你可以眼看出来,但是计算机是不知 道的,计算机只根据我们给予了它的指令才执行的)*/#include <stdio.h>#include <string.h>#include <stdlib.h>int str_compare(const ...
越悦璐3525 :答:C++里面,系统只要发现字符串,这是字符串就是以只针针的形式被识别的,就已经是一个地址了!!!举例if("banana"=="banana")cout<<""equal"<<endl;else cout<<"unequal"<<endl; 结果输出的是:unequal 因为系统识别两个内容相同的字符串时是识别的地址,if中的两个字符存储的位置不用,地...
越悦璐3525 :答:C语言3个字符串比较大小 我来答 1个回答 #热议# 哪些癌症可能会遗传给下一代?梓偃诼49 2021-05-06 · TA获得超过127个赞 知道答主 回答量:128 采纳率:66% 帮助的人:35.9万 我也去答题访问个人页 关注 展开全部 已赞过 已踩过< 你对这个回答的评价是? 评论 收起 为你推荐:特别推荐 幽...
越悦璐3525 :答:在C语言中比较字符串大小,可以使用库函数strcmp,也可以自己编写比较函数。1、使用库函数。需要包含头文件string.h。声明为:int strcmp(const char *s1, const char *s2);会根据字典序比较s1和s2, 如果二者相等,则返回0;如果s1较小则返回-1;如果s1较大则返回1。比如 strcmp("123", "123")...
越悦璐3525 :答:1、首先打开vc6.0, 新建一个项目。 2、添加头文件。 3、添加main函数。 4、定义x, y, z, t。 5、使用scanf给定义的变量赋值。 6、使用printf输入。 7、运行程序,可以看到输入的3个字符串,比较它们的大小,并将它们按由小到大的顺序输出。 已赞过 已踩过< 你对这个回答的评价是? 评论 收起 推荐...
越悦璐3525 :答:h>#include <string.h>#define MAX 100// 字符串最大长度// 交换两个字符串void swap(char *str1, char *str2){char temp[MAX];strcpy(temp, str1);strcpy(str1, str2);strcpy(str2, temp);}int main(){char str1[MAX], str2[MAX], str3[MAX];printf("请输入三个字符串:")...
越悦璐3525 :答:功能:比较字符串str1 and str2, 返回值如下:返回值 < 0 str1 < str2 = 0 str1 == str2 > 0 str1 > str2 include <stdio.h>#include <string.h>int main(){ char a[100], b[100], c[100]; printf("input 3 string :\n"); gets(a); gets(b); ...
越悦璐3525 :答:在C语言中,你可以使用不同的方式来比较字符串的大小。字符串的比较通常依赖于标准库函数和操作符。以下是一些常见的方法:使用strcmp函数:strcmp函数比较两个字符串的内容,返回一个整数值,表示比较结果。通常,如果字符串相等,返回值为0;如果第一个字符串小于第二个字符串,返回值为负数;如果第一...
越悦璐3525 :答:什么最大?字符串长度?ASC码总和?还是说是纯数字的字符串,作为数字来比较大小?我把几种情况都写了。你参考吧。多个相等的最大值,只返回其中1个 include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>int getsum(char *str);//获取字符串ASC总和char *getmax(int ...
越悦璐3525 :答:traps and pitfalls traps Press any key to continue / include <stdio.h>#include <string.h>int main() {char a[30],b[30],c[30],max[30];scanf("%s%s%s",a,b,c);if(strcmp(a,b) > 0) strcpy(max,a);else strcpy(max,b);if(strcmp(max,c) > 0) puts(max);else puts(...