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

问题描述:

WAP以将字符串的替代字符转换为大写。字符串的第一个字母必须是Capital。 I/P:我们是世界 O/P:我们是世界将字符串的替代字符转换为大写。字符串的第一个字母必须是Capital

+2

SO不是代码编写的服务。给它一个镜头,如果你有问题就回来,并提出一个问题。 – azurefrog

由于第一个字母大写,我们可以得出结论,在偶数位置字符串的每一个字母都会大写进行,但由于可能还有特殊字符,我们也必须记住这一点。您可以使用的示例算法如下:

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

将字符附加到另一个字符串,您可以显示输出。

+0

您好,它非常有助于找出答案。谢谢 –

公共静态无效的主要(字串[] 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)); 

}

+0

这样我找到了确切的解决方案。 –