My Wildcard Anagram Finder Has a Few Bugs

There is an error with my code, but I am not sure how to fix it. I am suppose to create a program that will check if the inputs given are anagrams, meaning that if every letter in each word is the same, then it will output 'A', but if they are not, it will output a "N'. Something else is that for the second input, it can have asterisks and they can substitute for a missing letter.

Sample Input:
hello world
dlr**lleh*

Desired Output:
A

Received Output:
N

Another example:

Sample Input:
asdfghjkl
lkjhgfdsa

Desired Output:
A

Received Output:
N

I was told that my code didn't loop 26 times and only looped 25 times. Also, I was told that my code disregarded the very last character.

I tried a lot of different methods, but none of them worked. I am looking for help debugging my code.

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
#include <iostream>
#include <string>

using namespace std;
int main () {

    bool same = true;
    string str1, str2;
    char arr1[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    int arr2[26], arr3[26], ast = 0;

    getline(cin, str1);

    for (int i = 0; i < 25; i++){
        arr2[i] = 0;
        arr3[i] = 0;
    }

    for (int j = 0; j < 25; j++) {
        for (int i = 0; i < str1.length() -1; i++) {
            if (str1[i] == arr1[j]) {
                arr2[j]++;
            }
        }
    }

    getline(cin, str2);

    for (int j = 0; j < 25; j++) {
        for (int i = 0; i < str2.length() -1; i++) {
            if (str2[i] == arr1[j]) {
                arr3[j]++;
            }
            else if (str2[i] == '*') {
                ast++;
            }
        }
    }

    for (int i = 0; i < 25; i++) {
        if (arr2[i] != arr3[i]) {
            ast--;
            if (ast < 0) {
                same = false;
            }
        }
    }

    if (str1.length() != str2.length()) {
        same = false;
    }

    if (same == true) {
        cout << 'A';
    }
    else {
        cout << 'N';
    }

    cin.get();
    return 0;

}


Thanks for the help.
Last edited on
Right off the bat I can tell you that those outer for loops do only iterate 25 times.

 
for(int i = 0; i < 25; i++)


It only continues as long as i is less than 25, meaning it only iterates through the range [0,24]. It never reaches the 25th, and last, index in your arrays. You could do either

1
2
3
4
5
for(int i = 0; i < 26; i++) //i < number of indices

//OR

for(int i = 0; i <=25; i++) 


EDIT:
Same goes for the iterations through the strings.
1
2
3
4
5
for (int i = 0; i < str2.length() -1; i++)

//should be

for (int i = 0; i < str2.length(); i++)


One last thing: You don't account for spaces within the input strings. This, as an example, will cause "hello world" and "dlr**lleh*" to not match.
Last edited on
Thank you, I have revised my code.

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
#include <iostream>
#include <string>

using namespace std;
int main () {

    bool same = true;
    string str1, str2;
    char arr1[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    int arr2[26], arr3[26], ast = 0;

    getline(cin, str1);

    for (int i = 0; i <= 25; i++){
        arr2[i] = 0;
        arr3[i] = 0;
    }

    for (int j = 0; j <= 25; j++) {
        for (int i = 0; i < str1.length(); i++) {
            if (str1[i] == arr1[j]) {
                arr2[j]++;
            }
        }
    }

    getline(cin, str2);

    for (int j = 0; j <= 25; j++) {
        for (int i = 0; i < str2.length(); i++) {
            if (str2[i] == arr1[j]) {
                arr3[j]++;
            }
            else if (str2[i] == '*') {
                ast++;
            }
        }
    }

    for (int i = 0; i <= 25; i++) {
        if (arr3[i] > arr2[i]) {
            same = false;
        }
        if (arr3[i] != arr2[i]) {
            ast--;
            if (ast < 0) {
                same = false;
            }
        }
    }

    if (same == true) {
        cout << 'A';
    }
    else {
        cout << 'N';
    }

    cin.get();
    return 0;

}
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
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;


const char WILD = '*';                         // wildcard character


//======================================================================


void upper( string &s )                        // force string to upper case
{ 
   for ( int i = 0; i < s.size(); i++ ) s[i] = toupper( s[i] );
}


//======================================================================


void removeChar( string &s, char c )           // remove character c from a string
{ 
   string::iterator iter = remove( s.begin(), s.end(), c );
   s.erase( iter, s.end() );               
}                                              


//======================================================================


bool isAnagram( string word1, string word2 )
{
   removeChar( word1, ' ' );                                        // remove spaces
   removeChar( word2, ' ' );
   if ( word1.size() != word2.size() ) return false;                // no point comparing if they are different lengths

   upper( word1 );                                                  // force to upper case
   upper( word2 );

   sort( word1.begin(), word1.end() );                              // sort
   sort( word2.begin(), word2.end() );

   removeChar( word2, WILD );                                       // strip any asterisks from word2

   return includes( word1.begin(), word1.end(), word2.begin(), word2.end() );   // anagram if remainder of word2 is in word1
}


//======================================================================


int main()
{
   string word1, word2;

   cout << "Input first word or phrase: ";                            getline( cin, word1 );
   cout << "Input second word or phrase (may contain asterisks): ";   getline( cin, word2 );

   if ( isAnagram( word1, word2 ) ) cout << "Anagram";
   else                             cout << "Not anagram";
}


Input first word or phrase: Donald Trump
Input second word or phrase (may contain asterisks): damp lord nut
Anagram


Input first word or phrase: Harry Potter
Input second word or phrase (may contain asterisks): *rtottei*
Not anagram


Input first word or phrase: Mickey Mouse
Input second word or phrase (may contain asterisks): yikes come**
Anagram
Last edited on
Topic archived. No new replies allowed.