[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

anagram program

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

649 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