-4

How to get the difference of letter in two string in Java?

        String s1 = "2 + 5 = 7";
        String s2 = "2 5 7";        
        String difference = StringUtils.difference(s1, s2);
        System.out.println(difference);

I want the output +1|=2 As + is next after first character and = is next after second character while checking the difference between the two string.

How can I implement this program in java?

Test
  • 25
  • 3
  • 1
    Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see what questions you can ask. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Jul 07 '21 at 18:19
  • 3
    What about the whitespaces? – User123 Jul 07 '21 at 18:19
  • 1
    Otherwise, just loop through characters and determine whether the current character is the same in both strings. If yes, continue, otherwise write the character in the first string that doesn't match to the console and eliminate it from the string. Continue the loop while there are characters. If you don't want whitespaces, you need to check for this in the loop as well. – User123 Jul 07 '21 at 18:21
  • 1
    Duplicate of: https://stackoverflow.com/q/12089967/642706 – Basil Bourque Jul 07 '21 at 21:26

1 Answers1

1

just check every character from the string in a loop, and if ch1 != ch2 you output the difference.

epushor
  • 66
  • 6
  • And if the characters are not equal, he needs to go to the next character in the first string, but he needs to stay on the same character in the second string. – User123 Jul 07 '21 at 18:38
  • just iterate trough the Strings at the same time using the same index. – epushor Jul 07 '21 at 18:58