Given the following inputs:
String s; |
Write a for loop that will print out the string in alternating cases, with the first letter being lowercase.
Keep in mind the following String methods:
- str.toUpperCase(); //make it uppercase
- str.toLowerCase(); //make it lowercase
DO NOT USE .charAt()! .charAt returns a character, and you need a string in order to make it upper/lowercase. To get a letter at a single position, use the following:
- str.substring(x,x+1); //gives character as string at position x
Sample input/outputs:
In: powerful out: pOwErFuL
In: COMEDIC out: cOmEdIc
In: acroBATics out: aCrObAtIcS |
HINT: You will need to use an IF statement inside your for loop!
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Bakjoon {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("In:");
String s = inp.nextLine();
//write your code below
for(int i=0;i<s.length();i++) {
System.out.print(i%2==0?s.substring(i,i+1).toLowerCase():s.substring(i,i+1).toUpperCase());
}
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
033 - For Loop Challenge 2 (optional) (0) | 2019.12.07 |
---|---|
032 - For Loop Challenge 1 (optional) (0) | 2019.12.07 |
030 - Further For Loop Practice 6 (reverse string) (0) | 2019.12.07 |
029 - Further For Loop Practice 5 (printing characters) (0) | 2019.12.07 |
028 - Further For Loop Practice 4 (repeating X times) (0) | 2019.12.07 |