aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab7/Functions.cpp
blob: de7de7f03b72dee57b32dd411cca3ca1f68d2d36 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "Header.h"

//getStringData definition
int getStringData(string data[])
{
    int cntGen = 0;

    while (cntGen < 99)
    {
        cout << "Enter string #" << cntGen+1 << ": ";
        cin >> data[cntGen];
            if (data[cntGen] == "done")
            {
                data[cntGen] = "\0";
                return 1;
            }

        cntGen++;
    }
}

//displayStrings definition
int displayStrings(string data[])
{
    //initializations
    int cntGen = 1;

    cout << endl;

    while (cntGen < 100)
    {
        cout << setw(10 + (data[cntGen - 1].size())) << data[cntGen - 1]
            << setw(20) << right << data[cntGen]
            << endl;

        cntGen = cntGen + 2;

        if (data[cntGen - 1] == "done")
        {
            return 1;
        }
        else if (data[cntGen] == "\0" || data[cntGen] == "done")
        {
            cout << setw(20) << right << data[cntGen - 1];
            return 1;
        }
    }
}


//prints out "match found" each time a letter of the user string is found.
//once a match for the first letter is found, then it moves onto the next, printing "match found" for each match
void findString(string data[])
{
    //initializations
    int cntData = 0;
    int cntDataChar;
    int cntUserChar;

    string user;
    
    //prompt
    cout << "\nEnter something to find whether it exists as a string or substring in the display: ";
    cin >> user;

    cout << endl;
    //calculate matches needed
    int match = user.size();

    //check
    while (data[cntData] != "\0")
    {
        //reset individual character counters and matches
        cntDataChar = 0;
        cntUserChar = 0;
        match = user.size();

        while (cntDataChar < data[cntData].size() && (cntUserChar < user.size() && cntDataChar < data[cntData].size()))
        {

            while (cntUserChar < user.size() && cntDataChar < data[cntData].size())
            {
                if (user[cntUserChar] == data[cntData][cntDataChar])
                {
                    if ((user[cntUserChar + 1] != data[cntData][cntDataChar + 1]) && (match < user.size() && match != 1))
                    {
                        cntDataChar++;
                        break;
                    }
                    else
                    {
                        match--;
                        cntUserChar++;
                    }
                    if (match == 0)
                    {
                        cout << "\n\"" << user << "\" has been found in the display"
                            << endl;
                        exit(0);
                    }
                }
                cntDataChar++;
            }
        }
        cntData++;
    }

    cout << "\n\"" << user << "\" has not been found in the display";
}