-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathShiftCharacters.java
More file actions
56 lines (54 loc) · 1.75 KB
/
ShiftCharacters.java
File metadata and controls
56 lines (54 loc) · 1.75 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
/**
* The class declaration for the second question
* Program to shift the characters of the string based on the index of a substring
*/
public class ShiftCharacters{
/**
* The function encode is used for shifting the string by 'k' characters
* 'k' is defined as the index of the substring sub in string s1
* @param sub is the substring
* @param s1 is the main string
* @return returns the final string after shifting the characters
*/
public static String encode(String sub, String s1){
int size = 0;
// Fetch positions of the sub-string in the Actual string
for (int i = 0; i < s1.length() - sub.length() + 1; i++) {
if (s1.substring(i, i + sub.length()).equals(sub)) {
size++;
}
}
int pos[] = new int[size];
int inc = 0;
for (int i = 0; i < s1.length() - sub.length() + 1; i++) {
// Condition to check if pattern matches
if (s1.substring(i, i + sub.length()).equals(sub)) {
pos[inc] = i;
++inc;
}
}
// Logic to increment the value of characters of the array
int count = 0;
char[] array = s1.toCharArray();
for(int i=0; i<s1.length(); i++){
if(count <= pos.length-1 && i==pos[count])
{
count++;
}
array[i]=(char)(s1.charAt(i) + count);
}
return String.valueOf(array);
}
/**
* The driver function for the program
* @param args
*/
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String sub = sc.nextLine();
sc.close();
System.out.println(encode(sub, s1));
}
}