c++比较字符串是否相等_在C++中比较字符串的3种方法

更新时间:2023-06-24 02:13:44 阅读: 评论:0

c++⽐较字符串是否相等_在C++中⽐较字符串的3种⽅法c ++⽐较字符串是否相等
In this tutorial, we’ll learn methods to compare strings in C++. Consider a scenario wherein you are required to enter your name and password to login to a particular website. In such cas, at the back end, we need to build and script functions to check and compare the input string (login details) with the string stored in the data ba.
在本教程中,我们将学习在C ++中⽐较字符串的⽅法。 考虑⼀种⽅案,其中要求您输⼊名称和密码才能登录到特定⽹站。 在这种情况下,在后端,我们需要构建和编写脚本函数,以检查输⼊字符串(登录详细信息)并将其与数据库中存储的字符串进⾏⽐较。
So, relating it here, in this article, will be understanding the Ways to compare string in .
因此,在本⽂中与此相关的将是理解在⽐较字符串的⽅法 。
在C ++中⽐较字符串的技术 (Techniques to Compare Strings in C++)
Strings in C++ can be compared using either of the following techniques:
可以使⽤以下两种技术之⼀来⽐较C ++中的字符串:
String strcmp() function
字符串strcmp()函数
In-built compare() function
内置compare()函数
C++ Relational Operators ( ‘==’ , ‘!=’ )
C ++关系运算符('==','!=')
1. C ++中的字符串strcmp()函数 (1. String strcmp() function in C++)
C++ String has got in-built functions to manipulate and deal with data of string type. In order to compare two strings, we can u String’s strcmp() function.
C ++ String具有内置函数来处理和处理字符串类型的数据。 为了⽐较两个字符串,我们可以使⽤String的strcmp()函数。
The strcmp() function is a C library function ud to compare two strings in a lexicographical manner.
strcmp()函数是C库函数,⽤于按字典顺序⽐较两个字符串。
Syntax:
句法:
int strcmp ( const char * str1, const char * str2 );
The function returns 0 if both the strings are equal or the same.
如果两个字符串相等或相同, 则该函数返回0 。
The input string has to be a char array of C-style string.
输⼊字符串必须是C样式字符串的char数组。
The strcmp() compares the strings in a ca-nsitive form as well.
strcmp()也以区分⼤⼩写的形式⽐较字符串。Example 1:
范例1:
#include<iostream>
using namespace std;
#include<string.h>
int main() {
const char *str_inp1="JournalDEV";
const char *str_inp2="JournalDEv";
cout<<"String 1:"<<str_inp1<<endl;
cout<<"String 2:"<<str_inp2<<endl;
if (strcmp(str_inp1, str_inp2) == 0)
cout << "\nBoth the input strings are equal." << endl;
el
cout << "The input strings are not equal.";
}
Output:
输出:
String 1:JournalDEV
String 2:JournalDEv
The input strings are not equal.
Example 2:
范例2:
缩句#include<iostream>
using namespace std;
#include<string.h>
int main() {
const char *str_inp1="Python";
ps如何截图const char *str_inp2="Python";
cout<<"String 1:"<<str_inp1<<endl;
cout<<"String 2:"<<str_inp2<<endl;
if (strcmp(str_inp1, str_inp2) == 0)
cout << "\nBoth the input strings are equal." << endl;
el
cout << "The input strings are not equal.";
}
Output:
输出:
String 1:Python
String 2:Python
Both the input strings are equal.
2. C ++中的compare()函数 (2. The compare() function in C++)
貔貅手链怎么戴
C++ has in-built compare() function in order to compare two strings efficiently.
C ++具有内置的compare()函数,以便有效地⽐较两个字符串。
The compare() function compares two strings and returns the following values according to the matching cas: compare()函数⽐较两个字符串,并根据匹配情况返回以下值:
Returns 0, if both the strings are the same.
如果两个字符串相同,则返回0。
Returns <0, if the value of the character of the first string is smaller as compared to the cond string input.
如果第⼀个字符串的字符值⼩于第⼆个字符串输⼊的字符,则返回<0。
江竹筠怎么读Results out to be >0, when the cond string is greater in comparison.
当第⼆个字符串⽐较⼤时,结果为> 0。
Syntax:
语法 :
int compare (const string& string-name) const;
Example 1:
范例1:
#include<iostream>
using namespace std;
int main() {
string str_inp1("Python");
string str_inp2("Python");
cout<<"String 1:"<<str_inp1<<endl;
投影面垂直线cout<<"String 2:"<<str_inp2<<endl;
int res = pare(str_inp2);
if (res == 0)
cout << "\nBoth the input strings are equal." << endl;
el if(res < 0)
cout << "String 1 is smaller as compared to String 2\n.";
el
cout<<"String 1 is greater as compared to String 2\n.";
}
In the above example, we have compared string 1 with string 2. As clearly en, both the strings are the same lexicographically, it returns 0.
在上⾯的⽰例中,我们将字符串1与字符串2进⾏了⽐较。可以清楚地看到,两个字符串在字典上都相同,返回0。
Output:
输出:
String 1:Python
String 2:Python
Both the input strings are equal.
Example 2:
范例2:
臀部穴位#include<iostream>
using namespace std;
int main() {
string str_inp1("Python");
string str_inp2("JournalDEV");
cout<<"String 1:"<<str_inp1<<endl;
if (pare("Python") == 0)
cout << "Strings are equal." << endl;
el
cout<<"Strings are not Equal\n.";
cout<<"\nString 2:"<<str_inp2<<endl;
if (pare("JournalDEv") == 0)
cout << "Strings are equal." << endl;
el
cout<<"Strings are not Equal.\n";
}
In the above snippet of code, we have compared a string with another input string to the compare() function directly.
在上⾯的代码⽚段中,我们将⼀个字符串和另⼀个输⼊字符串直接⽐较到compare()函数。
Output:
输出:
String 1:Python
Strings are equal.
String 2:JournalDEV
Strings are not Equal.
3. C ++中的关系运算符 (3. Relational Operators in C++)
C++ Relational operators such as ‘==’ and ‘!=’ can be uful in the comparison of string at an ea.
C ++关系运算符(例如'=='和'!=')在轻松⽐较字符串时很有⽤。
Syntax:
句法:
string1 == string 2
OR
string1 != string2
肝肾功能检查
Example 1: Using C++ ‘==’ operator
⽰例1:使⽤C ++'=='运算符
#include<iostream>
using namespace std;
int main() {
string str_inp1;
string str_inp2;
cout<<"Enter the String 1:\n";
cin>>str_inp1;
cout<<"Enter the String 2:\n";
cin>>str_inp2;
if (str_inp1 == str_inp2)
cout <<"Strings are equal"<<endl;
el
cout <<"Strings are not equal"<< endl;
}
Output:
输出:
涂唇膏
Enter the String 1:
Python
Enter the String 2:
PythoN
Strings are not equal
Example 2: Using C++ ‘!=’ operator
⽰例2:使⽤C ++'!='运算符

本文发布于:2023-06-24 02:13:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1052111.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   函数   处理   登录   名称   检查   运算符
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图