1String将自身附加在另一个C ++的末尾

时间:2020-03-07 22:13:34

标签: c++ string

如果两个字符串中连续有3个或更多类似的字母,我需要编写一个返回“ 1”的函数。

我编写的函数无论如何都返回“ 1”。

我试图打印出发现相似的字母,并发现编译器认为在第一个应该结束之后,它会匹配第二个字符串。 如果我尝试打印出str1,则输出为“ qwe”。 但是,如果我打印str1 [4],则输出为“ u”

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int Similar(char* str1, char* str2) {
  int i = 0, j = 0;
  for (i = 0; i < strlen(str1) + strlen(str2); i++) {
    for (j = 0; j < strlen(str1) + strlen(str2); j++) {
      if ((str1[i] == str2[j]) && (str1[i + 1] == str2[j + 1]) &&
          (str1[i + 2] == str2[j + 2])) {
        cout << str1[i] << " <1  2>" << str2[j]
             << endl;  // this is just for checking
        cout << str1[i + 1] << " <1  2>" << str2[j + 1]
             << endl;  // this is just for checking
        cout << str1[i + 2] << " <1  2>" << str2[j + 2]
             << endl;  // this is just for checking
        cout << i << " <1  2>" << j << endl;  // this is just for checking
        return 1;
      }
    }
  }
  return 0;
}

int main() {
  int size = 256;
  char str1[size] = {"qwe"}, str2[size] = {"uilo"};
  cout << Similar(str1, str2) << endl;
}

输出:

u<1   2>u

i<1   2>l

l<1   2>l

4<1   2>0

1

0 个答案:

没有答案