Help testing two words, then printing the cross of where the same chars are..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bill S

    #1

    Help testing two words, then printing the cross of where the same chars are..

    i'm trying to make a program that reads two English words from the command line, then outputs all possible ways the words can cross eachother. and print an error message if they do not cross. i want to use the charAt and length methods.. not sure where to even start.. thanks much.
  • Ian Smith
    New Member
    • Nov 2010
    • 6

    #2
    Use a for loop to loop through the letters in the first word and a nested for loop to loop through the second word. If the letters from the parent loop and child loop match, you have a place where the words can cross.

    To throw an error create a variable as true at the top of the two loops, when you get a match in the nested loop set the variable as true. If the variable is still false when you reach the bottom of the loop, there were no matches and you can throw an error there.

    Code:
    var = array
    flag = true
    for (word1)
      for (word2)
        if word1Char == word2Char
          Save position of loop1 and loop2 in array
          Set flag to false
    
    if flag == true
      throw error

    Comment

    Working...