?
Solved

anagram program

Posted on 2007-04-08
4
Medium Priority
?
354 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
Suggested Courses

850 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