Solved

anagram program

Posted on 2007-04-08
4
345 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

744 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

14 Experts available now in Live!

Get 1:1 Help Now