Friday, 2 March 2018

Remove Duplicate String From Two String

package com.tutorial4u;

public class DeleteRepetedCharacterFromTwoString {

     public static void main(String[] args) {
           String s1 = "Tutorial";
        String s2 = "TotalJava";
        System.out.println("String s1 is = " + s1 + " , String s2 is = " + s2);

        char c1[] = s1.toCharArray();
        char c2[] = s2.toCharArray();
       
        for (int i = 0; i < c1.length; i++) {
          
               boolean charMatch = false;
              
               for (int j = 0; j < c2.length; j++) {
                     if ((String.valueOf(c1[i])).toLowerCase().equals(
                                   ((String.valueOf(c2[j])).toLowerCase()))) {
                       charMatch = true;
                            break;
                     }
               }
               if (charMatch) {
                     s1 = s1.replaceAll(String.valueOf(c1[i]).toUpperCase(), "");
                     s1 = s1.replaceAll(String.valueOf(c1[i]).toLowerCase(), "");
                     s2 = s2.replaceAll(String.valueOf(c1[i]).toLowerCase(), "");
                     s2 = s2.replaceAll(String.valueOf(c1[i]).toUpperCase(), "");
               }
        }
        System.out.println("After deleted s1 = " + s1 + ",  after deleted s2 = " + s2);
     }
}



Output :


String s1 is = Tutorial , String s2 is = TotalJava
After deleted s1 = uri,  after deleted s2 = Jv

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...