convert the alternate characters of the string to upper case . The first letter of the string has to be Capital

后端 未结 2 837
情话喂你
情话喂你 2021-01-29 16:07

WAP to convert the alternate characters of the string to upper case . The first letter of the string has to be Capital. I/P:We are the worlD O/P: We ArE tHe WoRlD

相关标签:
2条回答
  • 2021-01-29 16:52

    public static void main(String[] args) { // TODO Auto-generated method stub

        String s="We are the worLD";
        System.out.println(s);
        int j=0;
        String otherstring=null;
        int length=s.length();
        for (int i=0;i<length;i++){
            j++;
            char ch=s.charAt(i);
            if(!Character.isAlphabetic(ch)){
                j--;
                otherstring+=ch;
            }
            if(j%2==0){
                ch=Character.toLowerCase(ch);
                otherstring+=ch;                
            }else{
                ch=Character.toUpperCase(ch);
                otherstring+=ch;
            }
        }
        System.out.println(otherstring.substring(4));
    

    }

    0 讨论(0)
  • 2021-01-29 16:56

    Since the first letter is upper case, we conclude that every letter of the string at even position will be made to upper case, but since there may be spece or special characters we will have to keep that in mind also. A sample algorithm you can use is:

    String x = jTextField1.getText();
    len = x.length();
    String otherstring;
    int j=0; //to be used as counter to check alternate char
    for (int i = 0;i<len;i++) {
        j++;
        char ch = x.charAt(i);
        if(!isalpha(ch)){
            j--; //not to consider non-letters
            otherString += ch;
        }
        if (j % 2 != 0) {
            Character.toLowerCase(ch));
            otherString += ch;
        }
        else{
            Character.toUpperCase(ch);
            otherString += ch;
        }
    }
    

    the characters are appended to the other string and you can display the output.

    0 讨论(0)
提交回复
热议问题