3.2—字符串—Implement strStr()

来源:互联网 发布:网络女主播圈子很乱 编辑:程序博客网 时间:2024/06/05 22:50
描述
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

#include<iostream>#include<string>#include<vector>using namespace std;int ImplementStr(string mainstr, string substr){int mainlegth = mainstr.size();int sublength =substr.size();for (int i = 0; i < mainlegth;i++){int k = i;int j;for (j = 0; j < sublength;j++){if (mainstr[k] == substr[j]&&k<mainlegth){k++;}elsebreak;}if (j == sublength)return i;}return -1;}int main(){string mainstr = "I LOVE SEU UNIVERSITY IN NANJING china!";string substr = "china!";int index = ImplementStr(mainstr, substr);cout << index << endl;}

原创粉丝点击