package abc;

import java.util.*;

public class TransformCharacters {
    public static String changeCharacters(String string) {
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
                if (Character.isUpperCase(ch))
                    string.replace(ch, '1');

        return string;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s = "oYoroCoroSor";


Task was to replace all capital letters with 1.
The logic i used seems fine, but i am not getting the desired output.
the function returns the same string which has been sent to it, I have used Simple print statements at all steps in changeCharacters function, but i am not getting any output on the consloe except the same string whihc I have sent to it.

What you've done is great, you have literally missed one tiny detail.

The replace method in String returns a new String object. Currently you are calling the method but not putting the result anywhere. So when you return the variable 'string', you return what you gave it.

Try using string = string.replace(ch,'1'); instead. This means you are then assigning the result of replacing the capital letters to your string variable.

Good Work. Well done

In future, if you get stuck, have a look at the java Documentation online. Google something like 'Java String doc' and it will come up. It's really useful

commented: It is working sir, Thank you so much and yeah I'll keep that in mind. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.