import java.util.Arrays;

public class InterchangedWordInString {
String[] outputArray = new String[2];

public String[] findInterchangedWords(String[] s1, String[] s2) {
for (int i = 0; i < s1.length; i++) { if (!s1[i].equals(s2[i])) { for (int j = i + 1; j < s1.length; j++) { if (s2[j].equals(s1[i]) && s1[j].equals(s2[i])) { System.out.println("Digits are interchanged"); outputArray[0] = s1[i]; outputArray[1] = s2[i]; break; } } } } return outputArray; } public static void main(String[] args) { String s1 = "one two three four five six"; String s2 = "one two six four five three"; String[] s1Split = s1.split(" "); String[] s2Split = s2.split(" "); InterchangedWordInString inter = new InterchangedWordInString(); System.out.println(Arrays.toString(inter.findInterchangedWords(s1Split,s2Split))); } }

Find interchanged words in a string using JAVA
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *