-1
#include "iostream"
#include "string"
#include <fstream>
#include <cctype>

using namespace std;

#define NO_OF_CHARS 256

bool checkAnagram(string a,string b);

// Function to print all pairs of anagrams

void findAllAnagrams(string str[], int n)

{

    // loop for all strings from array

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

        for (int j = i+1; j < n; j++)

            if (checkAnagram(str[i], str[j])) // check if two strings are anagrams

                cout << str[i] << " is anagram of " << str[j] << endl; // print if strings are anagrams

}

// function to check whether two strings are anagram or not //

bool checkAnagram(string string1, string string2)

{

    // Create two count arrays and initialize all values as 0

    int count[NO_OF_CHARS] = {0};

    int i;

    for (i = 0; string1[i] && string2[i]; i++)

    {

        count[string1[i]]++;

        count[string2[i]]--;

    }



    if (string1[i] || string2[i])

        return false;

    for (i = 0; i < NO_OF_CHARS; i++)

        if (count[i])

            return false;

    return true;

}


int main()

{

    string string_name;

    ifstream file_stream;

    int count = 0;



    file_stream.open("PrideAndPrejudice.txt"); // open file to read. your filename



    // while loop to count number of strings

    while (getline(file_stream, string_name , ' '))

    {



        count++;

        for (int i = 0, length = string_name.size(); i < length; i++)

        {

            string_name[i] = tolower(string_name[i]);



            // check whether parsing character is punctuation or not

            if (ispunct(string_name[i]))

            {

                string_name.erase(i--, 1); // if punctuation is found erase that character

                length = string_name.size(); // reduce size of string

            }

        }



    }

    file_stream.close();

    file_stream.open("PrideAndPrejudice.txt"); // open file again to point to start of file

    string wordArr[count]; // create word array to store the list of words read from file

    count = 0; // initialize count to 0

    while (getline(file_stream, string_name , ' '))

    {



        for (int i = 0, length = string_name.size(); i < length; i++)

        {

            string_name[i] = tolower(string_name[i]); // convert uppercase to lowercase

            // check whether parsing character is punctuation or not

            if (ispunct(string_name[i]))

            {

                string_name.erase(i--, 1);

                length = string_name.size();

            }



        }

        wordArr[count] = string_name; // store word in array

        count++;

    }



    findAllAnagrams(wordArr, count); // check all anagrams



    file_stream.close(); // close file stream



     return 0;

}

My code is giving no output, am I not reading in the file correctly?

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
bob
  • 1
  • 2
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. 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 bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Mar 27 '20 at 12:20
  • 1
    Why do you open the file 2 times? You can reset the stream to the beginning, – drescherjm Mar 27 '20 at 12:21
  • 1
    `Am I not reading the file correctly`. Since I can't see the file I can't tell. But one thing you are not doing is checking that you opened the file successfully. Always check that file open succeeds. – john Mar 27 '20 at 12:21
  • 1
    ***My code is giving no output*** Maybe your code has not read the file because its in the wrong folder. You should add code to check for that. With that said using the debugger would should have removed the guesswork on what is happening, – drescherjm Mar 27 '20 at 12:22
  • 1
    My second piece of advice for debugging is to use a small file where you know the answer at first. Don't start with a large file. – drescherjm Mar 27 '20 at 12:26
  • `string wordArr[count];` really is not valid `c++`. [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) a std::vector would have been better and valid in standard `c++` and you don't need to read the file twice then. – drescherjm Mar 27 '20 at 12:56

1 Answers1

0

This is part of the complete solution to find anagram between to strings:

/** ASSUMES THE IMPLEMENTATION OF quicksort METHOD TO SORT THE STRINGS.
 * return '1' if str1 & str2 are anagrams. '0' otherwise.
 */
int isanagram2(char *str1, char* str2)
{
    // If both are NULL. Return true
    if(str1 == NULL && str2 == NULL)
        return true;
    // If one of them is null & other not.
    if(str1 == NULL || str2 == NULL || strlen(str1) != strlen(str2))
        return false;
    quicksort(str1);
    quicksort(str2);
    // Compare char-by-char
    for(int i=0; str1[i] != '\0' ; i++)
        if(str1[i] != str2[i])
            return false;
    return true;
}
ozShpigel
  • 87
  • 2
  • 9