C++字符串实战下

一 找出字符串str中所有的abc

1 代码

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string str("babccbabcaabcccbabccabcabcabbabcc");
    int num = 0;
    size_t fi = str.find("abc", 0);    
    while (fi!=str.npos)
    {
        cout << fi << "   ";
        num++;
        fi = str.find("abc", fi + 1);
    }
    if (0 == num)
        cout << "not find!";
    cout << endl;
    return 0;
}

2 运行

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
1   6   10   16   20   23   29

二 find_first_of的测试

1 代码

#include<iostream>
#include<string>

using namespace std;

int main()
{
    cout<<"find_first_of test:"<<endl;
    //测试size_type find_first_of (charT c, size_type pos = 0) const noexcept;
    string str("babccbabcc");
    cout << str.find('a', 0) << endl;//1
    cout << str.find_first_of('a', 0) << endl;//1   str.find_first_of('a', 0)与str.find('a', 0)
    //测试size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
    string str1("bcgjhikl");
    string str2("kghlj");
    cout << str1.find_first_of(str2, 0) << endl;//从str1的第0个字符b开始找,b不与str2中的任意字符匹配;再找c,c不与str2中的任意字符匹配;再找g,
                                                //g与str2中的g匹配,于是停止查找,返回g在str1中的位置2
    //测试size_type find_first_of (const charT* s, size_type pos, size_type n) const;
    cout << str1.find_first_of("kghlj", 0, 20);//2   尽管第3个参数超出了kghlj的长度,但仍能得到正确的结果,可以认为,str1是和"kghlj+乱码"做匹配
    return 0;
}

2 运行

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
find_first_of test:
1
1
2

三 将字符串中所有的元音字母换成*

1 代码

#include<iostream>
#include<string>

using namespace std;

int main()
{
    std::string str("PLease, replace the vowels in this sentence by asterisks.");
    std::string::size_type found = str.find_first_of("aeiou");
    while (found != std::string::npos)
    {
        str[found] = '*';
        found = str.find_first_of("aeiou", found + 1);
    }
    std::cout << str << '\n';
    return 0;
}

2 运行

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
PL**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.

四 find_last_of的测试

1 代码

#include<iostream>
#include<string>

using namespace std;

int main()
{
    cout<<"find_last_of test"<<endl;
    //测试size_type find_last_of (const charT* s, size_type pos = npos) const;
    //目标串中仅有字符c与源串中的两个c匹配,其余字符均不匹配
    string str("abcdecg");
    cout << str.find_last_of("hjlywkcipn", 6) << endl;//5   从str的位置6(g)开始想前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5
    cout << str.find_last_of("hjlywkcipn", 4) << endl;//2   从str的位置4(e)开始想前找,e不匹配,再找d,d不匹配,再找c,c匹配,停止查找,
                                                      //    返回c在str中的位置5
    cout << str.find_last_of("hjlywkcipn", 200) << endl;//5   当第2个参数超出源串的长度(这里str长度是7)时,不会出错,相当于从源串的最后一
                                                        //    个字符起开始查找
    return 0;
}

2 结果

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
find_last_of test
5
2
5

五 find_first_not_of的测试

1 代码

#include<iostream>
#include<string>

using namespace std;

int main()
{
    //测试size_type find_first_not_of (const charT* s, size_type pos = 0) const;
    string str("abcdefg");
    cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3   从源串str的位置0(a)开始查找,目标串中有a(匹配),再找b,b匹配,再找c,c匹配,
//    再找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3
    return 0;
}

2 结果

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
3

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值