From camelCase to snake_case

Difficulty: ★ ★ ☆ ☆

The Great Coding Wars host many battles. For example, VIM versus Emacs (add in Nano for extra military action). Also the fight between spaces and tabs for indenting. Add to these conflicts a minor skirmish: whether to use camelCase or snake_case naming conventions.

I’m not a stickler for either method, and I often tend to mix both. Still, names for variables, functions, methods, and the lot are classified into two major naming schemes: camelCase and snake_case.

The camelCase convention uses mid-text capital letters to easily identify words. Because you can’t use spaces in a long, descriptive name, the capitals assist with readability. For example, purgeOldData() for a function that purges old data.

The snake_case convention also assists in identifying words in a name, with the words separated by underscores. For example, the variable terminal_status represents the terminal connection’s current status.

I use both methods in my code, and often I commit the sin of mixing them in a perverted manner: fix_Input() Regardless, I’m certain programmers and developers have their preferences and woe be to the humble coder who disagrees.

The challenge for this month’s Exercise is to write code that identifies the naming convention, camelCase or snake_case, and converts the name to the other convention. To avoid any weird names, I’ve provided a consistent list for you in sample code here:

2023_08_01-Lesson.c

#include <stdio.h>

int main()
{
    const int count = 7;
    char *variable[] = {
        "readInputMeter",
        "cyclical_redundancy_check",
        "bumpyRide",
        "search_for_node",
        "string_convert",
        "divideByZeroError",
        "giveUpAndExplode"
    };
    int x;

    for(x=0; x<count; x++ )
    {
        printf("%s\n",variable[x]);
    }

    return(0);
}

Your task is to modify this code so that it identifies naming the convention, then outputs the same name but in the other convention: snake_case to camelCase and camelCase to snake_case.

Here is a sample run of my solution:

           readInputMeter -> read_input_meter
cyclical_redundancy_check -> cyclicalRedundancyCheck
                bumpyRide -> bumpy_ride
          search_for_node -> searchForNode
           string_convert -> stringConvert
        divideByZeroError -> divide_by_zero_error
         giveUpAndExplode -> give_up_and_explode

Please try this Exercise on your own before you examine my solution.

4 thoughts on “From camelCase to snake_case

  1. Is it possible, in circumstances where you don’t know in advance how much you’ll need, to alloc a chunk of memory and then just free what you haven’t used?

    I think I could throw together some code that attempted to do so but it might not actually be doing what I think or hope it’s doing.

  2. Certainly. You malloc() a huge chunk. Then, providing you know how much memory is used, you can realloc() the chunk to a smaller size. I’ve never used realloc() to make a memory chunk smaller, but it should work.

    (Oh, Lord. Now I have to try it….)

    Yes, it works.

  3. That’s brilliant. Thank you. Using realloc never occurred to me. I had some vague idea of creating a separate pointer to an address within the memory from where I wanted to free from, and then just freeing that. Probably as stupid as it sounds.

    Anyway, I was thinking about a string builder for this problem as the result is always going to be larger or smaller than the original string, due to adding or removing underscores. To avoid using a realloc for each character it would be better to grab chunks of, say, 16 bytes whenever you need more, and then slice off any memory that isn’t used.

    Another approach might be to scan the string to figure out how many underscores need to be added or removed, alloc the exact required memory in one go, then loop through the string again, building up the new string as per the “rules”.

    I’ll try your approach sometime but I’m very busy at the moment. I’ll run it through valgrind to see if it approves or starts frowning and tutting.

  4. My solution just outputs the text, so nothing is stored. I did write a solution that allocates a liberal chunk of RAM to store the solution. I also wrote a solution that allocates an exact size chunk of memory. Posts coming up soon.

Leave a Reply