C++ string查找示例

时间:2014-02-25 17:21:11   收藏:0   阅读:305

查找两个字符串中的公共字符;

返回公共字符的索引;

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s = "**Gteate Wall**!";
	string t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	cout<<"s: "<<s<<endl;
	cout<<"t: "<<t<<endl;

	int first=s.find_first_of(t);

	if(first == string::npos){
		cout<<"s中所有字符均不在t中"<<endl;
	}else {
		cout<<"s中出现在t中的字符的第一个字符:"<<s[first]<<endl;
	}

	int last = s.find_last_of(t);
	if(last == string::npos){
		cout<<"s中所有字符均不在t中"<<endl;
		return 1;
	}else {
		cout<<"s中出现在t的字符的最后一个字符:"<<s[last]<<endl;
		return 1;
	}

}


原文:http://blog.csdn.net/slience_perseverance/article/details/19842027

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!