Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Task5 {
  6.  
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. System.out.println("Please enter two words.");
  10. System.out.println("First word: ");
  11. String firstWord = sc.nextLine();
  12. System.out.println("Second word: ");
  13. String secondWord = sc.nextLine();
  14.  
  15. //първата дума да е по-дългата, за да знаем до къде да сравняваме
  16. String temp = "";
  17. if(firstWord.length() < secondWord.length()) {
  18. temp = firstWord;
  19. firstWord = secondWord;
  20. secondWord = temp;
  21. }
  22.  
  23. boolean found = false;
  24. int index1 = 0; // индекс на общата буква в първата дума
  25. int index2 = 0; // индекс на общата буква във втората дума
  26. for (int i = 0; i < secondWord.length() - 1; i++) {
  27. if (!found) {
  28. for (int j = 0; j < secondWord.length(); j++) {
  29. if (firstWord.charAt(i) == secondWord.charAt(j)) {
  30. index1 = i;
  31. index2 = j;
  32. found = true;
  33. break;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. char [][] printResult = new char [firstWord.length()][secondWord.length()];
  40. if (!found) {
  41. System.out.println("The words don't have letter in common.");
  42. } else {
  43. for (int row = 0; row < firstWord.length(); row++) {
  44. for (int col = 0; col < secondWord.length(); col++) {
  45. if (row == index2 && col == index1) {
  46. printResult[row][col] = firstWord.charAt(index1);
  47. }
  48. if(col == index2) {
  49. printResult[row][col] = firstWord.charAt(row);
  50. }
  51. if (row == index1) {
  52. printResult[row][col] = secondWord.charAt(col);
  53. }
  54. }
  55. }
  56.  
  57. for (int rows = 0; rows < printResult.length; rows++) {
  58. for (int cols = 0; cols < printResult[rows].length; cols++) {
  59. System.out.print(printResult[rows][cols]);
  60. }
  61. System.out.println();
  62. }
  63. }
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement