简体   繁体   English

将字符串的替代字符转换为大写。 字符串的第一个字母必须是大写

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

WAP to convert the alternate characters of the string to upper case . WAP 将字符串的替代字符转换为大写。 The first letter of the string has to be Capital.字符串的第一个字母必须是大写。 I/P:We are the worlD O/P: We ArE tHe WoRlD I/P:我们就是世界 O/P:我们就是世界

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.字符被附加到另一个字符串,您可以显示输出。

public static void main(String[] args) { // TODO Auto-generated method stub public static void main(String[] args) { // TODO 自动生成的方法存根

    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));

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM