-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringBits.java
More file actions
27 lines (26 loc) · 788 Bytes
/
Copy pathstringBits.java
File metadata and controls
27 lines (26 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
Given a string, return a new string made of every other
char starting with the first, so "Hello" yields "Hlo".
*/
package codingbat;
import java.util.*;
public class stringBits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1, s2 = "";
System.out.println("Enter a string: ");
s1 = sc.nextLine();
System.out.println("length: "+s1.length());
if(s1.length() % 2 == 0){
for(int i = 0; i < s1.length(); i += 2){
s2 = s2 + s1.charAt(i);
}
}
else{
for(int i = 0; i < s1.length(); i += 2){
s2 = s2 + s1.charAt(i);
}
}
System.out.println("Result: "+s2);
}
}