忽略大小写比较字符串大小
题目:

数组意义:
a字符数组:输入的数组一。
b字符数组:输入的数组二。
int 类型的t:存放strcmp(a,b)的结果。
那再说说思路,因为
所以先统一为小写,再用strcmp就ok了。
上代码🐱🏍
#include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std; int main() { char a[100],b[100]; fgets(a,100,stdin); fgets(b,100,stdin); if(a[strlen(a)-1]=='\n') a[strlen(a)-1]=0;//把a数组的换行符改为0; if(b[strlen(b)-1]=='\n') b[strlen(b)-1]=0;//把b数组的换行符改为0; for(int i=0;a[i];i++)//在a[i]不等于0的情况下执行; { if(a[i]>'A'&&a[i]<'Z') a[i]+=32; } for(int i=0;b[i];i++)//在b[i]不等于0的情况下执行; { if(b[i]>'A'&&b[i]<'Z') b[i]+=32; } int t=strcmp(a,b); if(t==0) cout<<"="; else if(t<0) cout<<"<"; else cout<<">"; return 0; }
总结:
1.函数strcmp只能用于char字符串,可以对两个字符串按ACSLL码比较大小⇨strcmp(a,b);在a==b的情况下返回0,在a<b的情况下返回-1,在a>b的情况下返回1。
2.字符串数组a做遍历时还可以像这样写⇨
3.char整行输入用fgets,fgets(a,100,stdin);
a:要输入的字符串。
100:要输入多少个。
stdin:特定格式。

浙公网安备 33010602011771号