Replace all characters which matches with 1st character of string.

 Replace all characters which matches with 1st character of string.



Description:- 


with the help of this program we will be able to replace all characters with '$' which matches with 1st character of the string. Only condition is that we shouldn't replace the first character with whom we are matching it.


Code:-

public static String change_char_inString(String str) {
   
    // convert string into an char array

    char[] arr = str.toCharArray();

    // check for first index string find it and replace it with $ sign.

    char firstIndexChar = arr[0];

    for(int i=1;i<arr.length;i++){
        if(arr[i]==firstIndexChar){
            arr[i] = '$';
        }
    }
    // convert char array back to string and return it.
    return str.valueOf(arr);
}


Feedback:-


After reading this page if you have any feedback do let me know in comments; i will try to bring those things in my upcoming posts.
















Comments

Popular Posts