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