C 验证字符串2中是否存在字符串1
C++ Verify if string 1 exist in string 2
请帮助我!
它不起作用,我没有犯错!
有一个函数可以验证彼此中的字符串存在吗?谢谢 !!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mot1="abc";
string mot2="oooooabcooo";
int j=1;
while((j!=mot1.length())||(j!=0))
{
for(int i=0;i<=mot.length();i++)
{
if(mot1[i]==mot2[i])
{j++;}
else j=0;
}
}
if(j==mot1.length())
cout<<mot1<<" existe dans "<<mot2<<endl;
else
if(j=0)
cout<<"erreur";
return 0;
}
这是您要寻找的功能。
mot2.find(mot1); // also std::find
希望这会有所帮助。
有一个盒子工具,但是如果您寻找劳动编码,我对您的代码一无所知。相反,我以这种方式找到:
#include <iostream>
#include <string>
using namespace std;
int find(string mot1,string mot2)
{
bool found;
for(int j=0;j<mot2.length()-mot1.length()+1;j++)
{
found=true;
cout<<"comparing "<<mot1.c_str()<<" and "<<(mot2.c_str()+j)<<endl;
for(int i=0;i<mot1.length();i++)
if(mot1[i]!=mot2[i+j])
{
found=false;
break;
}
if(found)
return j;
}
return -1;
}
int main()
{
string mot1="abc";
string mot2="oooooabcooo";
cout<<find(mot1,mot2)<<endl;
return 0;
}
相关文章:
- 将文件复制到自定义位置,存在字符串转换问题
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 有没有办法将字符串添加到 Vector 中,但前提是它尚不存在?->C++
- C++ 查找字符串中存在的元素向量
- 与C++中的空字符串相比,突变仍然存在?
- 将字符串转换为整数类型T,检查是否存在溢出
- 如何检查二进制文件中是否存在字符串
- 是否存在像C++中那样带有分隔符的C#原始字符串
- 在向量中存储字符串存在 SEG 错误
- 给定字符串和单词 S 的列表,检查列表中是否存在 S
- 除了字符串或宏调用之外,是否存在任何情况,其中"(<"是有效的字符流?
- 查找字符串中存在的不同字符
- C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
- 控制一个字符串是否存在于另一个字符串中(以 C++ 为单位)
- 阵列不能两次将相同的名称保存,当要添加新字符串时,程序应检查以查看该名称是否已经存在
- C 验证字符串2中是否存在字符串1
- C unordered_map String键在存在时并不总是找到?字符串v char
- 什么是最快检查字符串数组中是否存在字符串的方法
- 验证字符串中是否存在无效字符
- 我如何缩短检查子字符串存在的多个条件