2

What I'm trying to do right now is take a user input, and check a text file to see if you can make any of the words in the text file out of the input entered.

For example, if I entered "noqmopu", the output would be: moo moon mop muon pun quo upon

My code is below. I've omitted the rest of the program since it's finished and would use up space. What I've tried to do is, if the inputted string is larger than the word that is being checked in the text file, it'll convert both to a char then compare each letter of the input to the word. If the characters match, a counter adds 1 to itself and the for loop breaks to prevent a letter being matched twice. Then, if the counter is equal to the length of the word, that would mean you could make out the word from the inputted string and it gets printed on the screen.

At the moment, if I type "noqmopu", all that gets printed out is 'n' (which is one of the words in the text file). I realize I'll have to limit the printed words to words with 3 characters or higher, but I'd like to know how to resolve this first. If there's another way of doing this, that'd be great, but I'd also like to know why this isn't working. I can't use any Systems.Collections. Thanks.

Martyax
  • 89
  • 7

1 Answers1

4

You're looping over the line twice:

for (int i = 0; i < line.Length; i++)
{
    for (int j = 0; j < line.Length; j++)
    {

I think one of those was supposed to be looping over the input

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • I think you're right. The first for should have had i < input2.Length. Now when I try this though, it prints a lot more words than it should; words that have letters that aren't even in the input. Guess I'm still missing something.... – Martyax Sep 09 '15 at 09:12
  • @Martyax whats an example of `line` and `input` that gives weird results. Your code is a little complicated for a simple task like you describe – Jamiec Sep 09 '15 at 09:14
  • Not sure you have fully accounted for repeat characters eg input "aaa" would match word "ant" – Graham Sep 09 '15 at 09:16
  • Well going with the input of "noqmopu" that I mentioned above, now I get bon bop con cop coup don fop go ho Hom hop Io ion Juno lo lop Lou m moan mob monk moo moon morn mot moun moun mour mow n nob nod non nor norm not noun now of ohm omen one onus open opiu opt opus or our out ow own ox p phon pion Po pod poem poi pol pomp pond pong pont pony pop pot poun pour pout pow pro prom pun quod romp Ron so son sop sou soup ton top von won wop yon you – Martyax Sep 09 '15 at 09:17
  • What value of `line` gives that result for `noqmopu` input? – Jamiec Sep 09 '15 at 09:21
  • 'line' gets the words from the text file from StreamReader myReader so I don't think it has a value really? @Graham I'm not sure either haha – Martyax Sep 09 '15 at 09:27
  • Looks like I got it! Along with the first for loop needing to be input2.Length, the case for repeat characters wasn't fully accounted either as mentioned. Adding a 'lineChar[j] == ' '; inside 'if(lineChar[j] == inputChar[i]) fixed the issue. Thanks! – Martyax Sep 09 '15 at 17:38