strstr()函数c++代码实现实例及运行结果

strstr()函数返回值是主串中字符子串的位置以后的所有字符

c++代码

#include <iostream>using namespace std;char *strStr(char *str,char *subStr)
{for(int i=0;str[i]!='\0';i++){int j=0;if(str[i]==subStr[j]){while(str[i++]==subStr[j++]){if(subStr[j]=='\0')return &str[i-j];}}}return NULL;
}int main()
{char *str="abcdefgh";char *subStr;char *newSubStr;cout<<"源字符串为"<<str<<endl;cout<<"请输入字符子串"<<endl;cin>>subStr;newSubStr=strStr(str,subStr);cout<<"字符子串后的字符串为"<<newSubStr<<endl;return 0;
}
运行结果




本文链接:https://my.lmcjl.com/post/8010.html

展开阅读全文

4 评论

留下您的评论.