有相同字符串的查找
#include <iostream>
using namespace std;
bool isContains1(char* name, char* str)
{
char* p = name;
while (*p != '\0') {
bool isSame = true;
for (int i = 0;; i++) {
if (str[i] == '\0') {
isSame = true;
break;
}
if (str[i] != p[i]) {
isSame = false;
break;
}
}
if (isSame) return true;
p++;
}
return false;
}
bool isContains2(char* name, char* str)
{
int cnt = 0, cnt2 = 0;
for (int i = 0; i < 100; i++) {
if ('\0' == *(str + i)) break;
cnt++;
}
for (int i = 0; i < 100; i++) {
if ('\0' == *(name + i)) break;
cnt2++;
}
if (cnt2 >= 2) {
for (int i = 0; i < cnt - 1; i++)
{
for (int j = 0; j < cnt2 - 1; j++) {
if ((*(name + j) == *(str + i)) && (*(name + j + 1) == *(str + i + 1)))
{
return true;
}
}
}
}
return false;
}
void findFriends(char* str)
{
FILE* fp = NULL;
fopen_s(&fp, "data.txt", "r");
if (NULL != fp) {
char name[100];
while (EOF != fscanf_s(fp, "%s", name, 100)) {
if (isContains1(name, str)) {
cout << name << endl;
}
}
fclose(fp);
}
}
int main()
{
char str[] = "aa";
findFriends(str);
}