![](/img/trans.png)
[英]Upper case conversion - convert the first letter of each word in the string to uppercase
[英]convert the alternate characters of the string to upper case . The first letter of the string has to be Capital
WAP 将字符串的替代字符转换为大写。 字符串的第一个字母必须是大写。 I/P:我们就是世界 O/P:我们就是世界
由于第一个字母是大写,我们得出结论,字符串中偶数位置的每个字母都将变为大写,但由于可能存在特殊字符或特殊字符,我们也必须牢记这一点。 您可以使用的示例算法是:
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;
}
}
字符被附加到另一个字符串,您可以显示输出。
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.