Open In App

Convert Camel Case String to Snake Case in C++

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Programming can be complicated, and it’s essential to ensure that our code is readable and well-structured. One way to improve code clarity is by using meaningful and descriptive variable and function names. To make every element name unique and more meaningful we use camel case and snake case are two widely used conventions for naming variables and functions. Let us check both of them

Snake Case

Snake case strings begin with lowercase and separate words with underscores where each word start with lowercase.

Example

event_listener

Camel Case

Camel case strings begin with lowercase and capitalize the first letter of subsequent words

Example: 

EventListener

Conversion from Camel Case to Snake case

In C++, we can easily convert camel case strings to snake case strings by replacing capital letters with underscores and the lowercase letters that follow them. In this article, we’ll delve into how to perform this conversion in C++.

Example:

Camel Case: GeeksForGeeks
Snake Case: geeks_for_geeks

Program to implement conversion

Given a string in camel case, the task is to write a C++ program to convert the given string from camel case to snake case and print the modified string.

Example

Input: GeeksForGeeks
Output: geeks_for_geeks

Approach of conversion

The approach to the topic is simple and easy to understand. The approach is mentioned below:

  • First, we initialize a variable ‘result’ with an empty string and append the first character (in lower case) to it.
  • Now iterate over each character of the string from the first index to the last index, if the character is in the upper case alphabet, we append ‘_’ and the character (in lower case) to the result, otherwise just append the character only.

Time Complexity of the Program

Time Complexity: O(n)
Auxiliary Space: O(n)

Implementation of the above topic:

C++




// C++ Program to convert 
// Snake case to Camel Case
#include <iostream>
#include <string>
  
using namespace std;
  
// Function to convert camel case
// string to snake case string
string camelToSnake(string str)
{
  
    // Empty String
    string result = "";
  
    // Append first character(in lower case)
    // to result string
    char c = tolower(str[0]);
    result+=(char(c));
  
    // Traverse the string from
    // ist index to last index
    for (int i = 1; i < str.length(); i++) {
  
        char ch = str[i];
  
        // Check if the character is upper case
        // then append '_' and such character
        // (in lower case) to result string
        if (isupper(ch)) {
            result = result + '_';
            result+=char(tolower(ch));
        }
  
        // If the character is lower case then
        // add such character into result string
        else {
            result = result + ch;
        }
    }
  
    // return the result
    return result;
}
  
int main()
{
    // Given string str
    string str = "GeeksForGeeks";
  
    // Print the modified string
    cout << camelToSnake(str);
  
    return 0;
}


Output

geeks_for_geeks


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads