-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheveryNth.java
More file actions
27 lines (24 loc) · 899 Bytes
/
Copy patheveryNth.java
File metadata and controls
27 lines (24 loc) · 899 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 non-empty string and an int N, return the string made starting
with char 0, and then every Nth char of the string. So if N is 3, use
char 0, 3, 6, ... and so on. N is 1 or more.
*/
package codingbat;
import java.util.*;
public class everyNth {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String str;
int n, limit;
System.out.print("Enter a string: ");
str = sc.nextLine();
System.out.print("Enter the repeating index value: ");
n = sc.nextInt();
limit = (int) java.lang.Math.ceil((float) (str.length()/(float) n));
StringBuilder result = new StringBuilder();
for(int i = 0; i < str.length(); i = i+n){
result.append(str.charAt(i));
}
System.out.print("Result: "+ result);
}
}