Advertisement
dimipan80

Exam 1. Cognate Words

Sep 10th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3.  
  4. public class _1_CognateWords {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         Scanner scan = new Scanner(System.in);
  9.         String inputLine = scan.nextLine().trim();
  10.  
  11.         String[] words = inputLine.split("[^A-Za-z]+");
  12.         HashSet<String> cognateWords = new HashSet<>();
  13.         int w1, w2;
  14.         String foundWords;
  15.         for (w1 = 0; w1 < words.length; w1++) {
  16.             for (w2 = 0; w2 < words.length; w2++) {
  17.                 if (w2 != w1) {
  18.                     for (String rightStr : words) {
  19.                         boolean isFoundCognateWords = words[w1].concat(
  20.                                 words[w2]).equals(rightStr);
  21.                         if (isFoundCognateWords) {
  22.                             foundWords = String.format("%s|%s=%s", words[w1],
  23.                                     words[w2], rightStr);
  24.                             cognateWords.add(foundWords);
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.  
  31.         if (cognateWords.isEmpty()) {
  32.             System.out.println("No");
  33.         } else {
  34.             for (String lineStr : cognateWords) {
  35.                 System.out.println(lineStr);
  36.             }
  37.         }
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement