Thread: Word Ladder

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Unhappy Word Ladder

    hey,
    im trying to create a word ladder but am getting stuck. Im able to get the words which differ by one character apart but now i dont know where to go from here..can someone please give me a hint or idea as to where i should go from here.. here is what i got so far..

    just in case you dont know a word ladder is ladder which connects from one word to another..example a ladder from code to data is
    code -> core -> care -> dare -> date -> data

    Code:
    #include "lexicon.h"
    #include "queue.h"
    #include "vector.h"
    #include <iostream>
    
    
    bool printOneAway(string,string,int,Lexicon);
    
    int main() {
    	string sWord,eWord;
    	Lexicon english("EnglishWords.dat");
    	
    	while(true)
    	{
    			cout << "Enter Start Word:";
    			cin >> sWord;
    			cout << "Enter End Word:";
    			cin >> eWord;
    			
    			for(int i = 0 ; i < sWord.size(); i++)
    			{
    				if(printOneAway(sWord,eWord,i,english))
    					break;
    				cout << endl;
    			}
    			
    	}
    	
    	return 0;
    }
    
    bool printOneAway(string first,string last, int position, Lexicon lex)
    {
    	Vector<string> ladder;
    	string temp = first;
    	ladder.add(first);
    	for(char c = 'a' ; c != 'z'; c++)
    	{
    
    		temp[position] = c;
    		if(lex.containsWord(temp) && temp != first)
    		{
    			ladder.add(temp);
    			if(temp == last)
    			{
    				for(int i = 0 ; i < ladder.size(); i++)
    					cout << ladder[i] << endl;
    				return true;
    			}
    				
    		}
    		
    	}
    	return false;
    }
    Last edited by rocketman03; 04-25-2009 at 11:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM