tianjiale

字符串的比较,使用strcmp()函数。

  一 .strcmp(A,B)函数接受两个字符串地址作为参数,意味着参数可以是指针,字符串常量或字符数组名。

    若str1==str2,则返回零;
  若str1<str2,则返回负数;
  若str1>str2,则返回正数。
  
 1 #include<iostream>
 2 #include<cstring>
 3 int main(){
 4     using namespace std;
 5     char word[5]="?ate";
 6 //strcmp字符串的比较
 7     for(char ch=\'a\';strcmp(word,"mate");ch++){
 8         cout<<word<<endl;
 9         word[0]=ch;
10     }
11     system("PAUSE");
12     return 0;
13 }

  结果为:

 

  二.比较String类字符串

 

#include<iostream>
#include<cstring>
int main(){
    using namespace std;
    string word="?ate";
//strcmp字符串的比较
    for(char ch=\'a\';word!="mate";ch++){
        cout<<word<<endl;
        word[0]=ch;
    }
    system("PAUSE");
    return 0;
}

 

 

 

结果一样和上面代码一样。

!=使用条件:至少有个操作数为string对象,另一个操作上可以是string对象,也可以C-风格字符串。string类的设计能够将string对象作为一个实体(在关系型测试表达式中),也可以将其作为一个聚合对象,从而使用数组表示法提取其中的字符。

 

 

 

 

 

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-04-06
  • 2021-11-05
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案