?
Solved

anagram program

Posted on 2007-04-08
4
Medium Priority
?
352 Views
Last Modified: 2013-12-14
I am working on an anagram program that does the following:
1)  sorts letters of each word alphabetically for initial array of keys
2)  print keys and words
3)  goes through the array of keys to find duplicated keys and prints them

Inside the bubbleSortLetters function, I am getting this error:  'swap' : cannot convert parameter 1 from 'char' to 'char *'

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::iostream;
using std::ostream;

#include <string>
using std::getline;

#include<iomanip>
using std::setw;

#include <cstring>
using std::string;

#include <fstream>
using std::fstream;
using std::ifstream;
using std::ofstream;


char** readWords( const char*, int& );
void bubbleSortLetters( char *, int );
void swap( char *, char * );
void bubbleSortWords( char **, char **, int );
void swapWords( char ** , char ** );
void print( char **, char **, int );

int main( )
{
      int numberOfWords;
      //char ** words = readWords( "words.txt", numberOfWords );
      char** words = readWords( "ReadWords.txt", numberOfWords );

      char** keys = new char* [ numberOfWords ];

      for (int j = 0; j < numberOfWords; j++ )
      {
            keys[ j ] = strdup( words[ j ] );
      }

      // create the keys array
      for (int index = 0; index < numberOfWords; ++index )
      {
            keys[ index ] = strdup( words[ index ] );
            bubbleSortLetters( keys[ index ], strlen( keys[ index ] ) );
      }

      bubbleSortWords( keys, words, numberOfWords );

      for ( int index = 0; index < numberOfWords; ++index )
      {
            cout << keys[ index ] << "  " << words[ index ] << "\n";
      }

      print( words, keys, numberOfWords );
      
      return 0;

}//end main

char** readWords( const char* fileName, int& numberOfWords )
{
      ifstream countingStream( fileName );
      // first count them
      numberOfWords = 0;
      while ( true )
      {
            char line[100];
            countingStream.getline( line, 100 );
            if ( strlen( line ) == 0 )
            {
                  break;
            }
            numberOfWords += 1;
      }
      countingStream.close( );

      ifstream readingStream( fileName );
      char** words = new char* [ numberOfWords ];
      for ( int index = 0; index < numberOfWords; ++index )
      {
            char line[100];
            readingStream.getline( line, 100 );
            words[index] = strdup( line );
            cout << line << "\n";
      }
      cout << endl;
      readingStream.close( );
      return words;
}

void bubbleSortLetters( char *letters, int length )      // sorts the letters of each word
{
      for ( int pass = 0; pass < length; pass++ )
      {
            for ( int j = 0; j < length - 1; j++ )
            {
                  if (letters[ j ] > letters[ j+1 ] )
                  {
                        swap( letters[ j ], letters[ j+1 ] );            //'swap' : cannot convert parameter 1 from 'char' to 'char *'
                  }
            }
      }
}

void swap( char *  wordLetter, char *  wordLetter2 )      // called by bubbleSort
{
   int temp = *wordLetter;
   *wordLetter = *wordLetter2;
   *wordLetter2 = temp;
}


void bubbleSortWords(char **keys, char **words, int wordCount)      // sorts the key words
{
   for ( int pass = 0; pass < wordCount; pass++ )
      {
         for ( int j = 0; j < wordCount - 1; j++ )
            {
              if ( strcmp( keys[ j ], keys[ j+1 ] ) > 0 )
              {
                        swapWords( &( keys[ j ] ), &( keys[ j+1 ] ) );
                        swapWords( &( words[ j ] ), &( words[ j+1 ] ) );
              }
        }
   }
}

void swapWords( char** left, char** right ) // called by bubbleSortWords
{
      char* temp;

      temp = *left;
      *left = *right;
      *right = temp;
}


void print( char **inputWords, char **keyWords, int numWords )
{      
      char* previousAnagram;
      previousAnagram = 0;
      bool anagram = false;

      for( int pass = 0; pass < numWords; pass++ )
      {
            if( ( previousAnagram != 0 ) && (strcmp( previousAnagram, keyWords[ pass ] ) != 0 ) )
            {
                  for( int match = pass + 1; match < numWords; match++ )
                  {
                        if( strcmp( keyWords[ pass ], keyWords[ match ] ) == 0 )
                        {
                              if( match == pass + 1 )
                              {
                                    cout << inputWords[ pass ] << " ";
                              }

                              cout << inputWords[ match ] << " ";
                              anagram = true;

                        }
                  }

                  if( anagram == true )
                  {
                        cout << "\n";
                        anagram = false;
                  }
            }

            previousAnagram = keyWords[ pass ];
      }

      cout << "\n";

}
0
Comment
Question by:CharleneS77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 18873130
                       swap( letters[ j ], letters[ j+1 ] );            //'swap' : cannot convert parameter 1 from 'char' to 'char *'

should be :

                        swap( &letters[ j ], &letters[ j+1 ] );
0
 

Author Comment

by:CharleneS77
ID: 18873209
Once again, you have opened my eyes.  Thank you!

If I use the following words in my text file:
rude
word
dure
from
form
letter

The final output of my program should be:
rude dure
from form

But for some reason, my output is only giving me:
from form

Can you see why I am not getting "rude dure"?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 18873273
I noticed that this loop in your main :

      for (int j = 0; j < numberOfWords; j++ )
      {
            keys[ j ] = strdup( words[ j ] );
      }

should be removed, because you do the same thing in the next loop.



But, that's not causing your problem (it "just" causes a memory leak).

The problem is in your print() function :

            if( ( previousAnagram != 0 ) && (strcmp( previousAnagram, keyWords[ pass ] ) != 0 ) )

should be :

            if( ( previousAnagram == 0 ) || (strcmp( previousAnagram, keyWords[ pass ] ) != 0 ) )
0
 

Author Comment

by:CharleneS77
ID: 18873616
That did it.  Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question