Thread: Word Unscrambler

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    20

    Word Unscrambler

    Hello, I am new to C++ and programming in general and am having a problem with a word unscrambler I'm trying to code. I have two txt files, words.txt and wordlist.txt that contain the scrambled words and unscrambled words respectively. As a test, my scrambled word is tac and my wordlist contains: dog bark cat. It finds the right word, but it has trouble unscrambling it.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int main()
    {
    	fstream list("wordlist.txt");
    	fstream words("words.txt");
    	string sWords, sList;
    	getline(words, sWords);
    	while(!list.eof())
    	{
    		getline(list,sList);
    		if(sWords.length()==sList.length())
    		{
    			for(int i=0; i<sWords.length();i++){
    				for(int k=0; k<sList.length();k++){
    					if(sWords[i]==sList[k]){
    						sWords[i]=sList[k];
    					}
    				}
    			}
    		}
    		cout << sWords << endl;
    		getline(words, sWords);
    	}
    	cin.get();
    	return 0;
    }
    My output is:
    Code:
    tac
    tac
    tac
    tac
    ... loops for a while >_>
    I've been trying for a long time and am wondering if someone could point me in the right direction. Thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Hint:
    count the number of occurances of each letter in each word.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM