Help check the two words, and then print the cross, where the same characters

I'm trying to make a program that reads two English words from the command line, and then prints out all the possible ways that words can cross each other. and print an error message if they do not overlap. I want to use the charAt and length methods. I don’t know where to even start ..

here is what i got so far:

public class Cross2
{
    public static void main(String[] args)
    {

    // create two dimentional array that stores input
    private String[][] cross = new String[w1pos][w2pos];

    // get input from user
    Scanner input = new Scanner(System.in);
    System.out.println("Enter two words: ");
    String word1 = input.next();
    String word2 = input.next();

    // loop through length of words
    for(int w1pos = 0; w1pos < word1.length(); w1pos++) {
        for(int w2pos = 0; w2pos < word2.length(); w2pos++) {

        // if characters are equal, print the array
        if (word1.charAt(w1pos) == word2.charAt(w2pos))
             System.out.println(cross[w1pos][w2pos]);
        }
    }
+3
source share
2 answers

I think so. I will need to skip the length of the string and then use charAt (i) to print each character.

This is a good start.

and yes intersection, as in matching letters .. so I need to compare each character using charAt for each position in each of the two words

. : , ?

,

:... ?

. . , .

+3

, "abra" "cadabra" ?

c   
abra
d   
a   
b   
r   
a   


c   
a   
d   
abra
b   
r   
a   


c   
a   
d   
a   
b   
r   
abra


 c  
 a  
 d  
 a  
abra
 r  
 a  


  c 
  a 
  d 
  a 
  b 
abra
  a 


   c
abra
   d
   a
   b
   r
   a


   c
   a
   d
abra
   b
   r
   a


   c
   a
   d
   a
   b
   r
abra

, , , . .

[:]

Java, , , , , .

// This is a comment explaining what the code does.

.

/* This is a comment explaining what you need to add to make the code work. */

.

public class Cross2
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter two words: ");
        String word1 = input.next();
        String word2 = input.next();
        String[][] cross = new String[word1.length()][word2.length()];
        /* Fill 'cross' with spaces */
        for(int w1pos = 0; w1pos < word1.length(); w1pos++) {
            for(int w2pos = 0; w2pos < word2.length(); w2pos++) {
                if (word1.charAt(w1pos) == word2.charAt(w2pos)) {
                    // Store 'word1' horizontally into 'cross'.
                    for(int w1posAgain = 0; w1posAgain < word1.length(); w1posAgain++) {
                        /* Store 'word1.charAt(w1posAgain)' horizontally into 'cross'
                        at row 'w2pos' and column 'w1posAgain'. */
                    }
                    // Store 'word2' vertically into 'cross'.
                    for(int w2posAgain = 0; w2posAgain < word2.length(); w2posAgain++) {
                        /* Store 'word2.charAt(w2posAgain)' vertically into 'cross'
                        at row 'w2posAgain' and column 'w1pos'. */
                    }
                    for(int w1posAgain = 0; w1posAgain < word1.length(); w1posAgain++) {
                        for(int w2posAgain = 0; w2posAgain < word2.length(); w2posAgain++) {
                            System.out.print(cross[w1posAgain][w2posAgain]);
                        }
                        System.out.println();
                    }
                    /* Fill 'cross' with spaces.
                    Yes, really.*/
                }
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1771129/


All Articles