Solved

anagram program

Posted on 2007-04-08
4
347 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
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 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 250 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now