0

I am making a program that loads a wordlist, then uses a function to find anagrams of an input word in the wordlist. I add these words to a vector.

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

int main()
{
    vector<string> words;
    vector<string> anagrams;
    string inputWord;
    words=loadWordlist("wordlist.txt");
    cout << "Find single-word anagrams for the following word: ";
    cin >> inputWord;
    anagrams = anagram(inputWord, words);
    for (int i=0; i<anagrams.size(); i++)
    {
        cout << anagrams[i] << endl;
    }
    return 0;
}

vector<string> loadWordlist(string filename)
{
    vector<string> words;
    ifstream inFile;
    string word;
    inFile.open(filename);
    if(inFile.is_open())
    {
        while(getline(inFile,word))
        {
            if(word.length() > 0)
            {
                words.push_back(word);
            }
        }
        inFile.close();
    }
    return words;
}

bool isAnagram(string word1, string word2){
    int n1=word1.length();
    int n2=word2.length();
    
    if(n1!=n2){
        return false;
    }
    
    sort(word1.begin(), word1.end());
    sort(word2.begin(), word1.end());
    
    for(int i=0; i<n1; i++){
        if(word1[i]!=word2[i]){
            return false;
        }
    }
    return true;
    
}

vector<string> anagram(string word, vector<string> wordlist)
{
    vector<string> anagramList;
    string compareWord;
    int size=wordlist.size();
    for(int i=0; i<size; i++){
        compareWord=wordlist[i];
        if(isAnagram(word, compareWord)==true){
            anagramList.push_back(compareWord);
        }
    }
    return anagramList;
}

The program is supposed to take an included text file(wordlists.txt) and create a vector of strings using the words from the list. It then uses this list of words and compares each word to a inputted word to check if they are anagrams. If they are anagrams, then the word from the wordlist is then added to the vector anagramList

I dont get a line number when it returns the fault, but I am sure the issue is somewhere in my anagram function. I always have issues with using vectors and trying to access their values, so any help is appreciated.

  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It runs your program, one line at a time, and shows you what's happening, including where it segfaults. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all problems in this and all future programs you write, without having to ask for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Oct 25 '20 at 21:11
  • Does it compile without any warnings? –  Oct 25 '20 at 21:15
  • @SamVarshavchik i used the debugger and from what i can see, I think the segementation fault comes in at ``` if(isAnagram(word, compareWord)==true) ``` I can't figure out what is wrong with this line, if i set a break to before this i can print the values of the two strings being compared, so I can't see why there would be troubl accessing anything in this line – ianspivey11 Oct 25 '20 at 21:32
  • yes, compiles with no warnings @Chipster – ianspivey11 Oct 25 '20 at 21:32
  • 1
    `sort(word2.begin(), word1.end());` That looks wrong. I'd imagine that's your problem. –  Oct 25 '20 at 21:36
  • Right, and if you step into this function with your debugger, you will discover that it blows up on this `sort()` line. – Sam Varshavchik Oct 25 '20 at 21:37
  • @SamVarshavchik oh, yeah that fixed it thanks a lot. how do i step into the funcction with my debugger? im using gdb. if i ran the program into the seg fault, it returns to me a wall of text with operators that I don't understand at all. By saying step into, do you mean there is a command I can use to watch the debugger run each line in the function one by one? Sorry, I'm still pretty new to coding and using a debugger is still confusing to me – ianspivey11 Oct 25 '20 at 21:41
  • In `gdb`, the `s` command steps into a function call. – Sam Varshavchik Oct 25 '20 at 21:51

0 Answers0