C/C++ Programming Learning-Week 9 ① Comparison of strings ignoring case

Topic link

Title description

Generally, we can use strcmp to compare the size of two strings. The comparison method is to compare the two strings character by character from the front to the back (compare according to the ASCII code value), until different characters appear or encounter'\0'. . If all characters are the same, they are considered the same; if there are different characters, the comparison result of the first different character shall prevail (Note: If a string encounters'\0', and another character If the string has not encountered'\0', the former is smaller than the latter).

But in some cases, when we compare the size of strings, we hope to ignore the size of letters. For example, "Hello" and "hello" are equal when ignoring the case of letters. Please write a program to compare the case of two strings, ignoring the case of letters.

Input format The
input is two lines, one character string per line, two character strings in total. (Each string is less than 80 in length and only contains uppercase and lowercase letters)

Output format
If the first string is smaller than the second string, output a character "<";

If the first string is larger than the second string, output a character ">";

If the two strings are equal, a character "=" is output.

Sample Input

Hellohowareyou
helloHowareyou

Sample Output

=

Ideas

Ignore the case of letters and compare the sizes of two strings.

If the first string is smaller than the second string, output a character "<";

If the first string is larger than the second string, output a character ">";

If the two strings are equal, a character "=" is output.

Two strings a and b can be defined. For the given two-line string, we read one character and one character in each line, turn all uppercase letters into lowercase letters, and then throw the processed characters into Among the strings we defined, we only need to compare two strings at the end. (Note: Strings in C++ can be directly compared, and character arrays in C language cannot be directly compared. The strcmp() function in the string.h header file must be used).

C++ code 1:

#include<iostream>
using namespace std;
int main()
{
    
    
	string a, b;		//定义字符串a,b
	char c;
	while(c = getchar())	//第一行中一个个地字符读取,放到字符串a
	{
    
    
		if(c == '\n') break;	//如果读到回车的话,就说明该行读取结束
		else if(c >= 'A' && c <= 'Z') c += 32;	//如果是大写字母的话,转换为小写字母。ASCII码表中每一个大写字母总是比对应的小写字母的值小32
		a.push_back(c);	//类似于python的函数使用,把字符变量c里面的字符放到字符串a的最后
	}
	while(c = getchar())	//第二行中一个个地字符读取,放到字符串b
	{
    
    
		if(c == '\n') break;	//如果读到回车的话,就说明该行读取结束
		else if(c >= 'A' && c <= 'Z') c += 32;//如果是大写字母的话,转换为小写字母。ASCII码表中每一个大写字母总是比对应的小写字母的值小32
		b.push_back(c);//类似于python的函数使用,把字符变量c里面的字符放到字符串b的最后
	}//字符串可以直接比较,直接比较输出即可
	if(a == b) cout << "=" << "\n";
	else if(a < b) cout << "<" << "\n";
	else cout << ">" << "\n";
	return 0;
}

C++ code 2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	char str1[205], str2[205];
	while(cin >> str1 >> str2)
	{
    
    
		int len1 = strlen(str1);
		int len2 = strlen(str2);
		for(int i = 0; i < len1; i++)
		{
    
    
			if(i == 0)
			{
    
    
				if(str1[i] >= 97 && str1[i] <= 122) str1[i] -= 32;
			}
			else
			{
    
    
				if(str1[i] >= 65 && str1[i] <= 90) str1[i] += 32;
			}
		}
		for(int i = 0; i < len2; i++)
		{
    
    
			if(i == 0)
			{
    
    
				if(str2[i] >= 97 && str2[i] <= 122) str2[i] -= 32;
			}
			else
			{
    
    
				if(str2[i] >= 65 && str2[i] <= 90) str2[i] += 32;
			}
		}
		if(strcmp(str1, str2) < 0) cout << "<" << endl;
		else if(strcmp(str1, str2) > 0) cout << ">" << endl;
		else if(strcmp(str1, str2) == 0) cout << "=" << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113095640