Write a for loop that will print out every other letter in a String, starting with the first letter. These letters should be printed all on one line with no space in between.
Sample input/outputs
In: hello out:hlo
In: so code very wow out:s oevr o |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Main {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("In:");
String word = inp.nextLine();
//write your code below
for(int i=0;i<word.length();i++) {
if (i%2!=0)
System.out.print("");
else {
System.out.print(word.charAt(i));
}
}
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
024 - For Loop 6 (optional) (0) | 2019.12.07 |
---|---|
023 - For Loops 5 (0) | 2019.11.30 |
021 - For Loops 3 (0) | 2019.11.30 |
020 - For Loops 2 (0) | 2019.11.30 |
019 - For Loops 1 (0) | 2019.11.30 |