Java program to convert snake case string to camel case

Given a snake case string, the task is to write a Java program that converts a given snake case string into a camel and outputs the modified string.

Examples:

Input: str = “geeks_for_geeks”
Output: GeeksForGeeks

Input: str = “snake_case_to_camel_case”
Output: SnakeCaseToCamelCase

Method 1: Using Traversal

  1. The idea is to capitalize the first letter of the string first.
  2. Then convert the string into a string constructor.
  3. Then move the string one character at a time from the first index to the last index and see if the character is underlined, then remove the next character and uppercase the character above the underscore.
  4. Print the changed string.

Below is the implementation of the above approach:

// Java program for the above approach

import java.io.*;

class GFG {

	// Function to convert snake case
	// to camel case
	public static String
	snakeToCamel(String str)
	{
		// Capitalize first letter of string
		str = str.substring(0, 1).toUpperCase()
			+ str.substring(1);

		// Convert to StringBuilder
		StringBuilder builder
			= new StringBuilder(str);

		// Traverse the string character by
		// character and remove underscore
		// and capitalize next letter
		for (int i = 0; i < builder.length(); i++) {

			// Check char is underscore
			if (builder.charAt(i) == '_') {

				builder.deleteCharAt(i);
				builder.replace(
					i, i + 1,
					String.valueOf(
						Character.toUpperCase(
							builder.charAt(i))));
			}
		}

		// Return in String type
		return builder.toString();
	}

	// Driver Code
	public static void
		main(String[] args)
	{

		// Given String
		String str = "geeks_for_geeks";

		// Function Call
		str = snakeToCamel(str);

		// Modified String
		System.out.println(str);
	}
}

Output:

GeeksForGeeks

Method 2: Using String.replaceFirst() method

  1. The idea is to use the String.replaceFirst() method to convert a specific string from falling snake to camel.
  2. First, capitalize the first letter of the string.
  3. Loops when the string contains an underscore (_).
  4. Replace the first occurrence of the letter that appears after the underscore with the uppercase letter of the next underscore.
  5. Print the changed string.

Below is the implementation of the above approach:

 

 
// Java program for the above approach

class GFG {

	// Function to convert the string
	// from snake case to camel case
	public static String
	snakeToCamel(String str)
	{
		// Capitalize first letter of string
		str = str.substring(0, 1).toUpperCase()
			+ str.substring(1);

		// Run a loop till string
		// string contains underscore
		while (str.contains("_")) {

			// Replace the first occurrence
			// of letter that present after
			// the underscore, to capitalize
			// form of next letter of underscore
			str = str
					.replaceFirst(
						"_[a-z]",
						String.valueOf(
							Character.toUpperCase(
								str.charAt(
									str.indexOf("_") + 1))));
		}

		// Return string
		return str;
	}

	// Driver Code
	public static void
	main(String args[])
	{
		// Given string
		String str = "geeks_for_geeks";

		// Print the modified string
		System.out.print(snakeToCamel(str));
	}
}

Output:

GeeksForGeeks

 

Submit Your Programming Assignment Details