-
Notifications
You must be signed in to change notification settings - Fork 368
/
Camel_Case.java
47 lines (46 loc) · 1.66 KB
/
Camel_Case.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
45
46
47
/*Name : Atul Kumar
Github username : atul1510
Repositary name : Algorithms
*/
/*Problem Description
Program to convert text sentence in CamelCase format .
Examples:
1] INPUT: "this is camel case"
OUTPUT "thisIsCamelCase"
2] INPUT: "Hi Atul here"
OUTPUT: "hiAtulHere"
Example Explanation */
import java.util.Scanner;
class Camel_Case {
// Function to remove spaces and convert into camel case
static String convert(String s) {
boolean flag = false;
int n = s.length();
char ch[] = s.toCharArray();
StringBuilder camelCase = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i == 0) camelCase.append(Character.toLowerCase(ch[0])); // Converts to lowercase
else if (ch[i] == ' ') flag = true; // Check for spaces in the sentence
else if (flag && ch[i] != ' ') {
camelCase.append(Character.toUpperCase(ch[i])); // Converts to uppercase
flag = false;
}
else camelCase.append(Character.toLowerCase(ch[i])); // Converts to lowercase
}
return camelCase.toString();
}
// Main function
public static void main(String args[]) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter the number of test cases: ");
int testCase = inp.nextInt();
inp.nextLine(); // Clear the input buffer
for (int i = 0; i < testCase; i++) {
System.out.print("Enter a sentence: ");
String str = inp.nextLine();
System.out.println("Original: " + str);
System.out.println("Camel Case: " + convert(str) + "\n");
}
inp.close();
}
}