C++

string 字符串比较以及c风格字符串比较

内容概要: C++ string comparison 字符串比较

#include<iostream>
#include<string>
using namespace std;

int  string_compare(string & s1, string & s2)
{
     if(s1 == s2)
     {
         return 0;
     }
     else if( s1 < s2)
     {
         return -1;
     }
     else
     {
         return 1;
     }
 }


int c_style_string_compare(const char *p1, const char *p2)
{
    if(*p1 < *p2 )
        return -1;
    else if(*p1 > *p2)
        return 1;
    else if (*p1 == '
#include<iostream>
#include<string>
using namespace std;
int  string_compare(string & s1, string & s2)
{
if(s1 == s2)
{
return 0;
}
else if( s1 < s2)
{
return -1;
}
else
{
return 1;
}
}
int c_style_string_compare(const char *p1, const char *p2)
{
if(*p1 < *p2 )
return -1;
else if(*p1 > *p2)
return 1;
else if (*p1 == '\0' && *p2 =='\0')
return 0;
else c_style_string_compare(p1+1,p2+1);
}
int main()
{
string a("abc");
string b("bcd");
string c("cde");
const char *p1 ="abc";
const char *p2 ="bcd";
const char *p3 ="cde";
const char *p4 = "aba";
cout<<string_compare(a,b)<<" "<<string_compare(b,c)<<endl;
cout<<c_style_string_compare(p1,p2)<<" " 
<<c_style_string_compare(p2,p3)<<endl;
cout<<c_style_string_compare(p1,p4)<<endl;
}
' && *p2 =='
#include<iostream>
#include<string>
using namespace std;
int  string_compare(string & s1, string & s2)
{
if(s1 == s2)
{
return 0;
}
else if( s1 < s2)
{
return -1;
}
else
{
return 1;
}
}
int c_style_string_compare(const char *p1, const char *p2)
{
if(*p1 < *p2 )
return -1;
else if(*p1 > *p2)
return 1;
else if (*p1 == '\0' && *p2 =='\0')
return 0;
else c_style_string_compare(p1+1,p2+1);
}
int main()
{
string a("abc");
string b("bcd");
string c("cde");
const char *p1 ="abc";
const char *p2 ="bcd";
const char *p3 ="cde";
const char *p4 = "aba";
cout<<string_compare(a,b)<<" "<<string_compare(b,c)<<endl;
cout<<c_style_string_compare(p1,p2)<<" " 
<<c_style_string_compare(p2,p3)<<endl;
cout<<c_style_string_compare(p1,p4)<<endl;
}
') return 0; else c_style_string_compare(p1+1,p2+1); } int main() { string a("abc"); string b("bcd"); string c("cde"); const char *p1 ="abc"; const char *p2 ="bcd"; const char *p3 ="cde"; const char *p4 = "aba"; cout<<string_compare(a,b)<<" "<<string_compare(b,c)<<endl; cout<<c_style_string_compare(p1,p2)<<" " <<c_style_string_compare(p2,p3)<<endl; cout<<c_style_string_compare(p1,p4)<<endl; }

============================================

C风格字符串比较通过递归实现,递归适于哪些需要重复某个固定的模式的过程
————————————————

本文同时发布在CSDN博客:
原文链接:https://blog.csdn.net/xiaoyaohuqijun/article/details/48199375

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top