-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifica_substring.cpp
More file actions
70 lines (65 loc) · 1.47 KB
/
Copy pathverifica_substring.cpp
File metadata and controls
70 lines (65 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Verificador de substring
#include <iostream>
#include <cstring>
#define MAX 50
using namespace std;
//funcao auxiliar de ehsubstring
bool testestring(char s1[], char s2[], int p)
{
for (int i = 0; i < strlen(s2); i++)
{
if (s1[p] != s2[i])
return false;
p++;
}
return true;
}
//funcao principal substring
bool ehsubstring(char s1[], char s2[])
{
for (int i = 0; i < strlen(s1); i++)
{
if (s1[i] == s2[0])
//cout << "achei ->" << s1[i];
{
if (testestring(s1, s2, i) == true)
{
return true;
}
}
}
return false;
}
bool ehsubstring2(char s1[], char s2[])
{
for (int i = 1; i < strlen(s1); i++)
{
if (s1[i] == s2[0])
//cout << "achei ->" << s1[i];
{
if (testestring(s1, s2, i) == true)
{
return true;
}
}
}
return false;
}
//verifica se a substring faz parte da string
int main()
{
//chamada da funcao
char string1[MAX], string2[MAX];
cout << "Insira uma frase: ";
cin.getline(string1, MAX);
cout << "Insira uma letra: ";
cin.getline(string2, MAX);
if (ehsubstring(string1, string2)){
if (ehsubstring2(string1, string2)){
cout << " e substring";
}
}
else
cout << " nao e substring";
return 0;
}