【发布时间】:2016-09-18 20:03:34
【问题描述】:
如果我有一个布尔原型,例如bool repeat(const char *S, char *P)
我想在 S 中搜索与 P 相同的序列,如果匹配则返回 true,例如:
char *this = "ABCDEFGH";
bool found;
found = count(this, "DEF"); // will be true
found = count(this, "FED"); // will be false
我目前的幼稚解决方案是
bool count (const char *S, char *P){
bool found;
int i = 0;
if (S[0] = P[0] && S[1] = P[1] && S[2] = P[2]) found = true;
else i + 1;
如果第一个成员不匹配,我可以使用语法 S[0 + i] 等继续查找数组吗?
感谢任何见解。谢谢。
【问题讨论】:
-
char *this = "ABCDEFGH";?这合法吗? -
@IInspectable 是的。见this post.
-
@AhmedAkhtar:不过,这个问题是关于c++,而
this是保留关键字。 -
@IInspectable 是的,你是对的。
-
@Lucas 看看我的答案,我已经发布了久经考验的工作代码。
标签: c++ arrays loops boolean sequence