Created
February 7, 2018 11:57
-
-
Save juanfal/eb6ba4f6e08b4426227219d9babf7820 to your computer and use it in GitHub Desktop.
words letters coinciding with a pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 04.pattern.cpp | |
// juanfc 2018-02-06 | |
// | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
const int PATLENGTH = 5; | |
const int NNONREPWORDS = 100; | |
typedef array<string,NNONREPWORDS> TWords; | |
string readPat(); | |
void addWord(TWords& ar, string w); | |
void printCoincidences(TWords ar, string pat); | |
int main() | |
{ | |
string pat = readPat(); | |
TWords ar; | |
string w; | |
cout << "Enter words, END to end" << endl; | |
while (cin >> w and w != "END") | |
addWord(ar, w); | |
printCoincidences(ar, pat); | |
return 0; | |
} | |
bool thereAreReps(string p); | |
string readPat() | |
{ | |
string p; | |
cout << "Pattern? "; | |
while (cin >> p and (p.size() != PATLENGTH or thereAreReps(p))) | |
cout << "Pattern? (" << PATLENGTH << " different letters) ";; | |
return p; | |
} | |
bool thereAreReps(string p) | |
{ | |
bool reps = false; | |
int i = 0; | |
while (not reps and i < p.size()) { | |
int j = i+1; | |
while (not reps and j < p.size()) { | |
reps = p[i] == p[j]; | |
++j; | |
} | |
++i; | |
} | |
return reps; | |
} | |
void addWord(TWords& ar, string w) | |
{ | |
int i = 0; | |
while (ar[i] != "" and ar[i] != w) | |
++i; | |
if (ar[i] == "") | |
ar[i] = w; | |
} | |
int noCoinc(string s, string p); | |
void printCoincidences(TWords ar, string pat) | |
{ | |
int i = 0; | |
while (ar[i] != "") { | |
cout << ar[i] << " " << noCoinc(ar[i], pat) << endl; | |
++i; | |
} | |
} | |
int noCoinc(string s, string p) | |
{ | |
int cnt = 0; | |
for (int j = 0; j < p.size(); ++j) { | |
int i = 0; | |
while (i < s.size() and p[j] != s[i]) | |
++i; | |
if (i < s.size()) | |
++cnt; | |
} | |
return cnt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment