void findMatches(char* keys[],char* words[], int& count)
{
for (int keyIndex = 0; keyIndex < count; ++keyIndex)
{
for (int keyIndex2 = 0; keyIndex2 < count; ++keyIndex2)
{
if ((keys[keyIndex]&& keys[keyIndex2]==0) && (words[keyIndex]&& words[keyIndex2]!=0)) ;
{
out <<keys[keyIndex]<<" "<<keys[keyIndex2]<<" "<<words[keyIndex2]<<endl;
}
}
}
}
void findMatches(char* keys[],char* words[], int& count)
{
for (int keyIndex = 0; keyIndex < count; ++keyIndex)
{
for (int keyIndex2 = 0; keyIndex2 < count; ++keyIndex2)
{
if ((keys[keyIndex]&& keys[keyIndex2]==0) && (words[keyIndex]&& words[keyIndex2]!=0))
{
out <<keys[keyIndex]<<" "<<keys[keyIndex2]<<" "<<words[keyIndex2]<<endl;
}
}
}
}
#include <iostream>
#include <fstream>
using namespace std;
char** read(const char* fileName, int& count);
char** wordArray(char* words[], int& count);
void createKeys(char* keys[], int& count);
void bubbleSort (char *keyArray , int count);
void findMatches(char* keyArray[],char* words[], int& count);
void displayResults(char* keys[],char* words[], int& count);
int main ()
{
int wordCount;
char** words = read("C:\\Users\\Mimi\\Desktop\\words.txt",wordCount);
char** keys = wordArray(words, wordCount);
createKeys(keys, wordCount);
findMatches(keys, words, wordCount);
//displayResults(keys, words, wordCount);
return 0;
}
char** read(const char* fileName, int& count)
{
ifstream countingStream(fileName);
// first count them
count = 0;
while (true)
{
char line[100];
countingStream.getline(line, 100);
if (strlen(line) == 0)
{
break;
}
count += 1;
}
countingStream.close();
ifstream readingStream(fileName);
char** words = new char* [count];
for (int index = 0; index < count; ++index)
{
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
readingStream.close();
return words;
}
char** wordArray(char* words[], int& count)
{
char** keyArray = new char*[count];
for(int index = 0; index < count; ++index)
{
keyArray[index] = _strdup(words[index]);
}
return keyArray;
}
void createKeys(char* keys[], int& count)
{
int length = 0;
for(int keysIndex = 0; keysIndex < count; ++keysIndex)
{
length = strlen(keys[keysIndex]);
bubbleSort(keys[keysIndex], length);
}
}
void bubbleSort (char *keyArray , int count)
{
char tempWord;
for (int passes = 0; passes < count - 1; passes++)
for (int compare = passes + 1; compare < count; compare++)
if (keyArray[passes] > keyArray[compare])
{
tempWord = keyArray[passes];
keyArray[passes] = keyArray[compare];
keyArray[compare] = tempWord;
}
}
void findMatches(char* keys[],char* words[], int& count)
{
for (int keyIndex = 1; keyIndex < count; ++keyIndex)
{
if (strcmp(keys[keyIndex -1], keys[keyIndex])!=0)
{
cout << keys[keyIndex] << " " << words[keyIndex]<< endl;
}
}
}
void displayResults(char* keys[],char* words[], int& count)
{
for (int keyIndex = 0; keyIndex < count; keyIndex++)
cout << keys[keyIndex] << " " << words[keyIndex] << endl;
}
#include <iostream>
#include <fstream>
using namespace std;
char** read(const char* fileName, int& count);
void selectionSort(int work[],const int size, bool(*compare)(int, int));
void swap(int * const element1Ptr, int * const element2Ptr);
bool ascending(int a, int b);
char** wordArray(char* words[], int& count);
void createKeys(char* keys[], int& count);
void bubbleSort (char *keyArray , int count);
void findMatches(char* keys[],char* words[], int& count);
int main ()
{
int count;
char** words = read("C:\\Users\\Mimi\\Desktop\\words.txt",count);
char** keys = wordArray(words, count);
createKeys(keys, count);
findMatches(keys,words,count);
return 0;
}
char** read(const char* fileName, int& count)
{
std::ifstream countingStream(fileName);
count = 0;
while (true) {
char line[100];
countingStream.getline(line, 100);
if (strlen(line) == 0) {
break;
}
count += 1;
}
countingStream.close();
std::ifstream readingStream(fileName);
char** words = new char* [count];
for (int index = 0; index < count; ++index) {
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
std::cout << line << std::endl;
}
readingStream.close();
return words;
}
void selectionSort(int work[],const int size, bool(*compare)(int, int))
{
int smallOrBig;
for( int i=0; i<size -1; i++)
{
smallOrBig =i;
for( int index=i+1; index<size ; index++)
if( !(*compare)(work[smallOrBig],work[index]))
smallOrBig = index;
swap(&work[smallOrBig],&work[i]);
}
}
void swap(int * const element1Ptr, int * const element2Ptr)
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
bool ascending(int a, int b)
{
return a<b;
}
char** wordArray(char* words[], int& count)
{
char** keyArray = new char*[count];
for(int index = 0; index < count; ++index)
{
keyArray[index] = _strdup(words[index]);
}
return keyArray;
}
void createKeys(char* keys[], int& count)
{
int length = 0;
for(int i = 0; i < count; ++i)
{
length = strlen(keys[i]);
bubbleSort(keys[i], length);
cout<< keys[i]<< endl;
}
}
void bubbleSort (char *keyArray , int count)
{
char tempWord;
for (int passes = 0; passes < count - 1; passes++)
for (int compare = passes + 1; compare < count; compare++)
if (keyArray[passes] > keyArray[compare])
{
tempWord = keyArray[passes];
keyArray[passes] = keyArray[compare];
keyArray[compare] = tempWord;
}
}
void findMatches(char* keyArray[],char* wordArray[], int& count)
{
for (int i = 1; i < count; ++i)
{
int key1 = (int)keyArray[i];
for (int j = 1; j < count; ++j)
{
int key2 = (int)keyArray[j];
if( key1.CompareTo(key2)==0)
{
cout << key1 << " " << key2<< endl;
}
}
}
}
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
char** read(const char* fileName, int& count);
char** buildArray(char* words[], int& count);
void getKeys(char* keys[], int& count);
void bubbleSortKeys (char *arrayKey , int count);
void findDuplicate(char* keys[],char* words[],char*dupkeyArray[], int& count);
void selectionSort(char* dupkeyArray[],char* words[]);
int main ()
{
int wordCount;
char** words = read("C:\\Users\\Mimi\\Desktop\\words[1].txt",wordCount);
char** keys = buildArray(words, wordCount);
getKeys(keys, wordCount);
char** dupkeyArray = new char *[wordCount];
findDuplicate(keys, words, dupkeyArray,wordCount);
selectionSort(dupkeyArray, words);
return 0;
}
char** read(const char* fileName, int& count)
{
ifstream countingStream(fileName);
count = 0;
while (true)
{
char line[100];
countingStream.getline(line, 100);
if (strlen(line) == 0)
{
break;
}
count += 1;
}
countingStream.close();
ifstream readingStream(fileName);
char** words = new char* [count];
for (int index = 0; index < count; ++index)
{
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
readingStream.close();
return words;
}
char** buildArray(char* words[], int& count)
{
char** keyArray = new char*[count];
for(int index = 0; index < count; ++index)
{
keyArray[index] = _strdup(words[index]);
}
return keyArray;
}
void getKeys(char* keys[], int& count)
{
int length = 0;
for(int keyIndex = 0; keyIndex < count; ++keyIndex)
{
length = strlen(keys[keyIndex]);
bubbleSortKeys(keys[keyIndex], length);
}
}
void bubbleSortKeys (char *arrayKey , int count)
{
char wordHold;
for (int pass = 0; pass < count - 1; pass++)
for (int compare = pass + 1; compare < count; compare++)
if (arrayKey[pass] > arrayKey[compare])
{
wordHold = arrayKey[pass];
arrayKey[pass] = arrayKey[compare];
arrayKey[compare] = wordHold;
}
}
void findDuplicate(char* keys[],char* words[],char*dupkeyArray[], int& count)
{
for (int i = 1; i < count; ++i)
{
for (int j = 1; j < count; ++j)
{
if ((strcmp(keys[i], keys[j])==0)&& (strcmp(words[i],words[j])!=0))
{
dupkeyArray [i]= keys[i];
cout.right;
cout << dupkeyArray[i]<<" "<< words[i]<<" \n";
}
}
}
}
void selectionSort(char* dupkeyArray[],char* words[])
{
int i, j;
char *temp; // holding variable
for (i=0; i< 32 ; i++) // element to be compared
{
for(j = (i); j < 32; j++) // rest of the elements
{
if (dupkeyArray[i] < dupkeyArray[j]) // descending order
{
temp= dupkeyArray[i]; // swap
dupkeyArray[i] = dupkeyArray[j];
dupkeyArray[j] = temp;
}
}
}
//for (i=0; i< 32 ; i++)
// cout << dupkeyArray[1]/*<<" "<< words[1]*/<<" \n";
}
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
char** read(const char* fileName, int& count);
char** keyArray(char* words[], int& count);
void createKeys(char* keys[], int& count);
void bubbleSortKeys (char *sortedKeys , int count);
void sortMatches(char* keys[],char* words[], int& count);
int main ()
{
int counter=0;
char** words = read("C:\\Users\\Mimi\\Desktop\\words[1].txt",counter);
char** keys = keyArray(words, counter);
createKeys(keys, counter);
sortMatches(keys, words,counter);
return 0;
}
char** read(const char* fileName, int& count)
{
ifstream countingStream(fileName);
// first count them
count = 0;
while (true)
{
char line[100];
countingStream.getline(line, 100);
if (strlen(line) == 0)
{
break;
}
count += 1;
}
countingStream.close();
ifstream readingStream(fileName);
char** words = new char* [count];
cout<<" Loading...\n "<<fileName<<"\n File is Loaded...\n There are "<<count<<" Records...\n\n";
for (int index = 0; index < count; ++index)
{
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
readingStream.close();
return words;
}
char** keyArray(char* words[], int& count)
{
char** keyArray = new char*[count];
for(int index = 0; index < count; ++index)
{
keyArray[index] = _strdup(words[index]);
}
return keyArray;
}
void createKeys(char* keys[], int& count)
{
int length = 0;
for(int keyIndex = 0; keyIndex < count; ++keyIndex)
{
length = strlen(keys[keyIndex]);
bubbleSortKeys(keys[keyIndex], length);
}
}
void bubbleSortKeys (char *sortedKeys , int count)
{
char wordHold;
for (int pass = 0; pass < count - 1; pass++)
for (int compare = pass + 1; compare < count; compare++)
if (sortedKeys[pass] > sortedKeys[compare])
{
wordHold = sortedKeys[pass];
sortedKeys[pass] = sortedKeys[compare];
sortedKeys[compare] = wordHold;
}
}
void sortMatches(char* keys[],char* words[], int& count)
{
cout<<" KEY"<<" WORD"<<" WORD\n";
cout<<"---------------------------------\n";
for (int i = 1; i < count; ++i)
{
for (int j = 1; j < count; ++j)
{
if ((strcmp(keys[i], keys[j])==0)&& (strcmp(words[i],words[j])!=0))
{
char*dup1[1];// I know this could be tighter but i'm not sure how to do it yet
char*dup2[1];
char*dup3[1];
dup1[i-1]=keys[i];
dup2[i-1]=words[i];
dup3[i-1]=words[j];
cout<<dup1[i-1]<<" "<<dup2[i-1]<<" "<<dup3[i-1]<<"\n";
}
}
}
}
#include <iostream>
#include <fstream>
#include <string.h>
#include <list>
using namespace std;
char** read(const char* fileName, int& count);
char** buildArray(char* words[], int& count);
void getKeys(char* keys[], int& count);
void bubbleSortKeys (char *arrayKey , int count);
void sortMatches(char* keys[],char* words[], int& count);
int main ()
{
int wordCount;
cout<<"\n This Program finds Anagrams in a List of Words \n";
cout<<"____________________________________________________\n\n";
char** words = read("C:\\Users\\Mimi\\Desktop\\words[1].txt",wordCount);
char** keys = buildArray(words, wordCount);
char** unique= new char*[wordCount] ;
getKeys(keys, wordCount);
sortMatches(keys, words, wordCount);
return 0;
}
char** read(const char* fileName, int& count)
{
ifstream countingStream(fileName);
// first count them
count = 0;
while (true)
{
char line[100];
countingStream.getline(line, 100);
if (strlen(line) == 0)
{
break;
}
count += 1;
}
countingStream.close();
if(count==0)
{
cout<<"\n Loading...\n "<<fileName<<"\n\n File is Empty or did not Load...\n There are "<<count<<" Records...\n Check Path or File Name\n\n";
}
else
{
cout<<"\n "<<fileName<<" is loaded...\n There are "<<count<<" Records.\n\n";
}
ifstream readingStream(fileName);
char** words = new char* [count];
for (int index = 0; index < count; ++index)
{
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
readingStream.close();
return words;
}
char** buildArray(char* words[], int& count)
{
char** keyArray = new char*[count];
for(int index = 0; index < count; ++index)
{
keyArray[index] = _strdup(words[index]);
}
return keyArray;
}
void getKeys(char* keys[], int& count)
{
int length = 0;
for(int keyIndex = 0; keyIndex < count; ++keyIndex)
{
length = strlen(keys[keyIndex]);
bubbleSortKeys(keys[keyIndex], length);
}
}
void bubbleSortKeys (char *arrayKey , int count)
{
char wordHold;
for (int pass = 0; pass < count - 1; pass++)
for (int compare = pass + 1; compare < count; compare++)
if (arrayKey[pass] > arrayKey[compare])
{
wordHold = arrayKey[pass];
arrayKey[pass] = arrayKey[compare];
arrayKey[compare] = wordHold;
}
}
void sortMatches(char* keys[],char* words[], int& count)
{
cout<<" Anagrams from Text File\n";
cout<<"---------------------------\n";
char** unique= new char*[count] ;
char** unique2= new char*[count] ;
for (int i = 1; i < count; ++i)
{
for (int j = 1; j < count; ++j)
{
if ((strcmp(keys[i], keys[j])==0)&& (strcmp(words[i],words[j])!=0))
{
unique[i]=keys[i];
cout<<" ";
int counter=counter+1;
cout.width(4);
cout<<left<<counter;
cout.width(8);
cout<<left<< words[i];
cout.width(8);
cout<< words[j]<<endl;
}
}
}
}
Second, perhaps the problem can be solved by using more parenthesis. Â For instance instead of
(keys[keyIndex]&& keys[keyIndex2]==0)
try
(( keys [ keyIndex ] &&Â keys [ keyIndex2 ] ) == 0 )
Assuming what I've done is what you mean to do.