import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String words[] = in.nextLine().toLowerCase().split(" ");
for (int i = 0; i < words.length; i++) {
if (i % 2 == 0) {
System.out.print(words[i].toUpperCase() + " ");
} else {
System.out.print(words[i].toLowerCase() + " ");
}
}
in.close();
}
}
Do this programming without using array and split
import java.util.Scanner;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String word = in.nextLine().toLowerCase();
int s = 0;
int n = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ' || i == word.length() - 1) {
System.out.print((n % 2 == 0 ? word.substring(s, i + 1).toUpperCase() : word.substring(s, i + 1)));
s = i + 1;
n++;
}
}
in.close();
}
}
Comments
Leave a comment