1 Replies - 679 Views - Last Post: 30 January 2009 - 01:13 PM

#1 linuxunil   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 07-March 06

Word unscramble

Posted 13 October 2006 - 08:37 AM

Description: Runs from command line with the wordlist argument given and the scrambled words in a file named scrambledwords.txt.Unscrambles words using a worldlist. Inspired form a challenge on hackthissite.org.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
   vector<string> wordlist;
   vector<string> scrambledwords;
   vector<string> sortedwordlist;
   string temp;
   int count;
   ifstream fin;
   
   fin.open("scramble.txt");
   while(!fin.eof()) {
      fin >> temp;
      scrambledwords.push_back(temp);
   }
      
   fin.close();
   fin.open("wordlist.txt");
   
   wordlist.resize(0);
   while(!fin.eof()) {
      fin >> temp;
      wordlist.push_back(temp);
   }
   sortedwordlist.resize(0);
   for( unsigned int i = 0; i < wordlist.size(); i++) {
      sortedwordlist.push_back(wordlist[i]);
   }
   
   
   
   for(unsigned int i = 0; i < sortedwordlist.size(); i++) {
      sort(sortedwordlist[i].begin(), sortedwordlist[i].end());
   }
   
   for( unsigned int i = 0; i < scrambledwords.size(); i++) {
      sort(scrambledwords[i].begin(), scrambledwords[i].end());
   }

   for(unsigned int i = 0; i < sortedwordlist.size(); i++) {
      for( unsigned int j = 0; j < scrambledwords.size(); j++) {
         if(scrambledwords[j] == sortedwordlist[i]) {
            cout << wordlist[i] << ",";
         }
      }
   }
   
   
   return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Word unscramble

#2 ls_nerd   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 23
  • Joined: 30-January 09

Re: Word unscramble

Posted 30 January 2009 - 01:13 PM

Hi, that's some nice code you wrote there, I really like it! It's short and well structured, just one look at it and anyone (who knows some c++) instantly understands it. I just wrote my own for that challenge and wanted to compare. However mine is C#.net (using windows Forms) and I didn't spent too much time on it, so one could propably cut some lines here and there... Because there is no sort function (at least not that I know of) for .net languages i had to run a lot of nested for loops. ;D
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Wordunscrambler
{
    public partial class Form1 : Form
    {
        public ArrayList wlist = new ArrayList();
        public Form1()
        {
            InitializeComponent();
            StreamReader wlist_reader = new StreamReader("wordlist.txt");
            while (!wlist_reader.EndOfStream) {
                wlist.Add(wlist_reader.ReadLine());                
            }
            wlist_reader.Close();            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String to_unsc = richTextBox1.Text;
            String[] to_unscarr;
	    richTextBox2.Text = "";

            if (to_unsc.Contains("n"))
            {
                to_unscarr = to_unsc.Split("nr".ToCharArray());
            }
            else {
                label3.Text = "Wrong format of input";
                return;
            }
            
            for (int i = 0; i < to_unscarr.Length; i++) {
                
                if(to_unscarr[i].Contains("#")){
                    to_unscarr[i] = to_unscarr[i].Split("#".ToCharArray())[1].Trim();
                }else{
                    label3.Text = "Wrong format of input";
                    break;
                }
                int loscrambled = to_unscarr[i].Length;
                
                for (int j = 0; j < wlist.Count; j++) {
                    
                    int lowlist = wlist[j].ToString().Length;
		    //compare length of words first to enhance performace
                    if (loscrambled == lowlist)
                    {
                        
                        Char[] tempwlist = wlist[j].ToString().ToCharArray();
                        Char[] temptounsc = to_unscarr[i].ToCharArray();
                        int countequals = 0;

                        for (int k = 0; k < lowlist; k++) {
                            for (int l = 0; l < lowlist; l++) {
                                if (temptounsc[k] == tempwlist[l])
                                {
                                    countequals++; tempwlist[l] = """.ToCharArray()[0];
                                    break;
                                }
                            }
                        }
                        if (countequals == lowlist) richTextBox2.Text += wlist[j] + ",";
                    }
                }
            }
        }
    }
}

I'd really like to hear your opinion... Greetings Lukas
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1