-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.java
44 lines (38 loc) · 1.27 KB
/
Hash.java
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
/*Question 1
Problem Statement –
You have write a function that accepts, a string which length is “len”, the string has some “#”, in it you have to move all the hashes to the front of the string and return the whole string back and print it.
char* moveHash(char str[],int n);
example :-
Sample Test Case
Input:
Move#Hash#to#Front
Output:
###MoveHashtoFront */
import java.util.Scanner;
public class Hash {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String d = sc.nextLine();
int len = d.length();
char[] a = new char[len];
char[] b = new char[len];
char[] c = new char[len];
// Store the input string as an array of characters
for (int i = 0; i < len; i++) {
a[i] = d.charAt(i);
}
int j = 0, k = 0;
// Separate the characters into two arrays: hashes and non-hashes
for (int i = 0; i < len; i++) {
if (a[i] == '#') {
c[j++] = '#';
} else {
b[k++] = a[i];
}
}
// Combine the two arrays into a single string with hashes at the beginning
String result = new String(c) + new String(b);
System.out.println(result);
sc.close();
}
}