How to resolve the algorithm Camel case and snake case step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Camel case and snake case step by step in the V (Vlang) programming language

Table of Contents

Problem Statement

Two common conventions for naming of computer program variables are Snake Case and Camel Case. Snake case variables are generally all lower case, with an underscore between words in the variable, as in snake_case_variable'. Camel case variables are generally lower case first (except in some Pascal conventions or with class names in many other languages), with captalization of the initial letter of the words within the variable, as in 'camelCaseVariable'. Leading underscores are not used in such variables except as part of a different naming convention, usually for special internal or system variables. White space is not permitted as part of camel case or snake case variable names.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Camel case and snake case step by step in the V (Vlang) programming language

Source code in the v programming language

fn to_camel(snake string) string {
    mut camel := ''
    mut underscore := false
    letters := snake.trim(' ').runes()
    for c in letters {
        if c.str() in [' ','-','_'] {
            underscore = true
        } else if underscore {
            camel += c.str().to_upper()
            underscore = false
        } else {
            camel += c.str()
        }
    }
    return camel
}
fn to_snake(camel string) string {
    mut snake := ''
    mut first := true
    letters := camel.trim(' ').replace(' ','_').runes()
    for c in letters {
        if first {
            snake+=c.str()
            first = false
        } else if !first && (c.str().is_upper() && c.bytes().len ==1 && c.bytes()[0].is_letter()) {
            if snake[snake.len-1..snake.len] == '_' || snake[snake.len-1..snake.len] == '-' {
                snake += c.str().to_lower()
            }else {
                snake += '_'+c.str().to_lower()
            }
        }else{
            snake+=c.str()
        }
    }
    return snake
}
const tests = ["snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
    "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", "  spaces  "]
fn main() {
    println('                          === to_snake_case ===')
    for word in tests {
        println('${word:33} -> ${to_snake(word)}')
    }
    println('                          === to_camel_case ===')
    for word in tests {
        println('${word:33} -> ${to_camel(word)}')
    }
}

  

You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Perl programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Cut a rectangle step by step in the C programming language
You may also check:How to resolve the algorithm Host introspection step by step in the C++ programming language