Hello friends, I need urgent help

0

The problem is the following, when I receive a word I must create a code where the word returns but in uppercase and lowercase example-- paneton - PaNeToN. Here I leave my intent, I only capture the letters of odd order of the word and I turn them into capital letters.

public void palabra(View v) {
    String p = "",im="",ip="",m="";
    int k=0;
    String a = et1.getText().toString();
    int z=p.length();
    for(int i=0;i< z;i++){
        p=a.substring(i,i+1);
        k=i%2;
        if(k==0){
            m=m+p;
        }
        im=m.toUpperCase();
    }
    tv1.setText(im);
}
    
asked by Edgar Alexander Hurtado Moloch 20.11.2018 в 02:45
source

1 answer

0

First: try to give significant names to the variables.

Second: variable z will always be zero because p initializes it with an empty string and when you reach the% for_co_de% is already zero.

Third: the variable z always ends up being capitalized.

Fourth: the variable m is the one that you should apply the p based on the result of toUpperCase , let's say you need an else.

I leave a function for you to try it, analyze it and compare it with what you have plus the observations that you leave above.

public String IntervaloMayusculas(String str)
{
    String tempStr = str.toLowerCase();
    String newStr = "";

    for (int i = 0; i < tempStr.length(); i++) {
        if(i % 2 == 0)
            newStr += Character.toUpperCase(tempStr.charAt(i));
        else
            newStr += tempStr.charAt(i);
    }

    return newStr;
}
    
answered by 20.11.2018 в 03:22