字符串匹配

来源:互联网 发布:数据库可以没有主键吗 编辑:程序博客网 时间:2024/06/06 07:13
/************************************************************************/
/* 
题目标题:
判断短字符串中的所有字符是否在长字符串中全部出现
详细描述:
接口说明
原型:
boolIsAllCharExist(char* pShortString,char* pLongString);
输入参数:
char* pShortString:短字符串
char* pLongString:长字符串


输入:输入两个字符串。(不知道哪个长,哪个短)
输出: true  - 表示短字符串中所有字符均在长字符串中出现
false- 表示短字符串中有字符在长字符串中没有出现
例 输入 bc abc
输出 true                                                                     */

/************************************************************************/



#include <iostream>#include <string>using namespace std;/************************************************************************//* 得到公共字符串的长度                                                 *//************************************************************************/bool isMatch(string shortStr, string longStr){int count = 0;for (int i = 0; i < shortStr.size(); ++i) //短字符串,把短字符串中每个字符和长字符串中每个进行比较,相等则停止,比较下一个字符{for (int j = 0; j < longStr.size(); ++j){if (shortStr[i] == longStr[j]){count++;break; //找到之后就跳出循环}}}if (count==shortStr.size()){return true;}else{return false;}}int main(){string input;string first;string second;string::size_type Lposition = 0;  //使用size_typestring::size_type Rposition = 0;bool IsMatch = false;cout << "请输入字符串:" << endl;while (getline(cin, input)) //死循环,可以一直测试{Lposition = input.find(" ");//从左边开始找空格Rposition = input.rfind(" ");//从右边开始找空格 这种方式就不管连个字符串中间有多少空格first.assign(input, 0, Lposition);//第一个参数:字符串  第二个参数:起始位置 第三个参数:长度second.assign(input, Rposition + 1, input.size() - Rposition);if (first.size()>second.size()) //短的字符串在前面{IsMatch = isMatch(second, first);}else{IsMatch = isMatch(first, second);}cout << "短字符串中所有字符均在长字符串中出现过:" << endl << ((IsMatch>0)?"true":"false") << endl << endl;}return 0;}


0 0