字符串相关函数的应用

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

int main() {
    char a[50] = "hello";
    char b[50];

    int  aLength = strlen(a);//求字符串长度
    cout << "aLength=" << aLength << endl;

    strcpy(b, a);//将字符数组a拷贝给b
    cout << "b=" << b << endl;

    strcat(a,b);//将b连接到a
    cout << "strcat result=" << a << endl;
  
    int cmp = strcmp(a, b);//从字符数组开头开始,依次比较每个字符,直到出现差异、或者其中一个串结束为止。
    cout << "strcmp result=" << cmp << endl;

}
#include<string>
#include <iostream>
using namespace std;
int main()
{
    string str1 = "Hello";     //初始化string类型,并具体赋值
    string str2 = " world!";
    
    int  str1Length = str1.length();
    cout << "str1Length=" << str1Length << endl;

    string str3 = str1 + str2; 
    cout << "add result="<<str3 << endl;

    int cmp = str1 > str2 ? 1 : str1==str2 ? 0:-1; //从字符串左边开始,依次比较每个字符,直到出现差异、或者其中一个串结束为止。
    cout << "compare result=" << cmp << endl;

    string str4 = str1.substr(2);
    cout << "substr(2)=" << str4 << endl;

    string str5 = str1.substr(1, 3);
    cout << "substr(1,3)=" << str5 << endl;//ell

    int index = str1.find('l', 0);
    cout << "find index=" << index << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值