ravikrishna89
ravikrishna89

Reputation: 53

how to delete a particular character at particular position from a character array ?

I need to delete a character from a character array and re-size the array. Until now, I have worked on replacing a particular character with a special character.

In this code I am searching for any matches that are found i.e , if any characters are matching in the male and female character array and if found I am replacing it with "*". Instead of that I have to delete that character and resize the array.

private static void Compare(String Male,String Female) {

    char[] male;
    char[] female;
    // converting a string into charecter array

    male=Male.toCharArray();
    female=Female.toCharArray();

    //finding any matches i.e, any charecter is matching or not 
    for(int i=0;i<male.length;i++){

        for(int j=0;j<female.length;j++)
        {
            String m = Character.toString(male[i]);
            String fm = Character.toString(female[j]);
            if(m.equals(fm)){
            //if the charecters are equal  then  replacing them with "*"    

                male[i]='*';
                female[j]='*';

            }
        }
    }

Upvotes: 3

Views: 667

Answers (4)

Exceptional
Exceptional

Reputation: 3004

You can achieve this easily using the following function:

String original_str="HOLLOW POOL";

String newone=original_str.replace("O", "*");     //Replaces the char "O" with "*"

System.out.print(newone);

The result will be H*LL*W P**L

That's it!!

Upvotes: 0

Joe0
Joe0

Reputation: 153

Well, there are quite a few concerns with your code. Namely, the lack of conventions, no return value, and little to no clarity on what the problem actually is.

Anyway, this will remove duplicates only if the character appears in both strings.

private static void compare(String male, String female) {
    List<Character> male1 = addAll(male.toCharArray());
    List<Character> female1 = addAll(female.toCharArray());

    for (int i = 0; i < male1.size(); i++) {
        Character c = male1.get(i);
        if (female1.contains(c)) {
            while (male1.remove(c)) {
                i--;
            }
            while (female1.remove(c));
        }
    }

    // Do whatever else here, unless you need a char array, then convert it
    // if you need to.
    char[] maleA = toArray(male1);
    char[] femaleA = toArray(female1);

    System.out.println(new String(maleA));
    System.out.println(new String(femaleA));
}

Here are the methods referenced in the above code.

private static char[] toArray(List<Character> list) {
    char[] array = new char[list.size()];
    for (int i = 0; i < array.length; i++) {
        array[i] = list.get(i);
    }
    return array;
}

private static List<Character> addAll(char[] array) {
    List<Character> list = new ArrayList<Character>();
    for (char c : array) {
        list.add(c);
    }
    return list;
}

If you need to use other data types, just make a generic version, or change it.

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94479

You could use the ArrayUtils class within the org.apache.commons.lang library to make this a little easier: Array Utils Link

    public static void main(String[] args) {
        String Male = "male";
        String Female = "female";
        char[] male;
        char[] female;
        // converting a string into charecter array

        male=Male.toCharArray();
        female=Female.toCharArray();

        //finding any matches i.e, any charecter is matching or not 
        for(int i=0;i<male.length;){
        boolean remove = false;
            for(int j=0;j<female.length;)
            {
                String m = Character.toString(male[i]);
                String fm = Character.toString(female[j]);
                if(m.equals(fm)){
                //if the charecters are equal  then  replacing them with "*"    
                   female =  ArrayUtils.remove(female, j);
                   remove = true;
                }else{
                    j++;
                }
            }
            if(remove)
            {
                male =  ArrayUtils.remove(male, i);
            }else{
                i++;
            }
        }
        System.out.println(male);
        System.out.println(female);

}

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63698

Try this:

    String male = "maleg*$m-z";
    String female= "femal^\\$e-is";
    String deletedMale = male.replaceAll("["+Pattern.quote(female)+"]", "");
    String deletedFemale = female.replaceAll("["+Pattern.quote(male)+"]", "");
    System.out.println(deletedMale);
    System.out.println(deletedFemale);

Upvotes: 3

Related Questions