Oración Caso de una string Camel dada

Dada una string str en formato camelCase , la tarea es convertir la string en una forma legible.
Ejemplos: 
 

Entrada: str = “ILoveGeeksForGeeks” 
Salida: Me encantan los geeks por geeks
Entrada: str = “WeLoveToCode” 
Salida: Nos encanta codificar 
 

Enfoque: la string está en mayúsculas, lo que significa que las palabras no están separadas por espacios, sino que cada letra mayúscula significa que ha comenzado una nueva palabra. Para mejorar su legibilidad, lo convertimos en caso de oración. A continuación se muestra el código para la conversión. Atravesamos la string y luego, cada vez que encontramos una minúscula, la imprimimos como tal. Cuando encontramos una letra mayúscula, generamos un espacio seguido del carácter en mayúscula (después de convertirlo a minúscula) y luego el resto de los caracteres.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the original string
// after converting it back from camelCase
void getOrgString(string s)
{
 
    // Print the first character as it is
    cout << s[0];
 
    // Traverse the rest of the
    // characters one by one
    int i = 1;
    while (i < s.length()) {
 
        // If current character is uppercase
        // print space followed by the
        // current character in lowercase
        if (s[i] >= 'A' && s[i] <= 'Z')
            cout << " " << (char)tolower(s[i]);
 
        // Else print the current character
        else
            cout << s[i];
        i++;
    }
}
 
// Driver code
int main()
{
    string s = "ILoveGeeksForGeeks";
 
    getOrgString(s);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
    // Function to return the original string
    // after converting it back from camelCase
    static void getOrgString(String s)
    {
     
        // Print the first character as it is
        System.out.print(s.charAt(0));
     
        // Traverse the rest of the
        // characters one by one
        int i = 1;
        while (i < s.length())
        {
     
            // If current character is uppercase
            // print space followed by the
            // current character in lowercase
            if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
                System.out.print(" "+ Character.toLowerCase(s.charAt(i)));
     
            // Else print the current character
            else
                System.out.print(s.charAt(i));
                 
            i++;
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
                String s = "ILoveGeeksForGeeks";
                getOrgString(s);
    }
}
 
// This code is contributed by AnkitRai01

Python

# Python3 implementation of the approach
 
# Function to return the original string
# after converting it back from camelCase
def getOrgString(s):
 
 
    # Print the first character as it is
    print(s[0],end="")
 
    # Traverse the rest of the
    # characters one by one
    i = 1
    while (i < len(s)):
 
        # If current character is uppercase
        # prspace followed by the
        # current character in lowercase
        if (ord(s[i]) >= ord('A') and ord(s[i] )<= ord('Z')):
            print(" ",s[i].lower(),end="")
 
        # Else print the current character
        else:
            print(s[i],end="")
        i+=1
 
# Driver code
 
s = "ILoveGeeksForGeeks"
 
getOrgString(s)
 
# This code is contributed by mohit kumar 29

C#

// C# implementation of the approach
using System;
     
class GFG
{
 
    // Function to return the original string
    // after converting it back from camelCase
    static void getOrgString(String s)
    {
     
        // Print the first character as it is
        Console.Write(s[0]);
     
        // Traverse the rest of the
        // characters one by one
        int i = 1;
        while (i < s.Length)
        {
     
            // If current character is uppercase
            // print space followed by the
            // current character in lowercase
            if (s[i] >= 'A' && s[i] <= 'Z')
                Console.Write(" "+ char.ToLower(s[i]));
     
            // Else print the current character
            else
                Console.Write(s[i]);
                 
            i++;
        }
    }
     
    // Driver code
    public static void Main (String[] args)
    {
                String s = "ILoveGeeksForGeeks";
                getOrgString(s);
    }
}
 
/* This code is contributed by PrinciRaj1992 */

Javascript

<script>
      // JavaScript implementation of the approach
      // Function to return the original string
      // after converting it back from camelCase
      function getOrgString(s)
      {
       
        // Print the first character as it is
        document.write(s[0]);
 
        // Traverse the rest of the
        // characters one by one
        var i = 1;
        while (i < s.length)
        {
         
          // If current character is uppercase
          // print space followed by the
          // current character in lowercase
          if (
            s[i].charCodeAt(0) >= "A".charCodeAt(0) &&
            s[i].charCodeAt(0) <= "Z".charCodeAt(0)
          )
            document.write("  " + s[i].toLowerCase());
             
          // Else print the current character
          else document.write(s[i]);
          i++;
        }
      }
 
      // Driver code
      var s = "ILoveGeeksForGeeks";
      getOrgString(s);
       
      // This code is contributed by rdtank.
    </script>
Producción: 

I love geeks for geeks

 

Publicación traducida automáticamente

Artículo escrito por ritikadhamija y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *