Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created February 7, 2018 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanfal/eb6ba4f6e08b4426227219d9babf7820 to your computer and use it in GitHub Desktop.
Save juanfal/eb6ba4f6e08b4426227219d9babf7820 to your computer and use it in GitHub Desktop.
words letters coinciding with a pattern
// 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