Dada la string str , la tarea es convertir la string dada en su abreviatura de la forma: primer carácter, número de caracteres entre el primero y el último carácter y el último carácter de la string.
Ejemplos:
Entrada: str = “internacionalización”
Salida: i18n
Explicación: Primera letra ‘i’, seguida del número de letras entre ‘i’ y ‘n’, es decir, 18, y la última letra ‘n’.Entrada: str = «geeksforgeeks»
Salida: g11s
Enfoque: el problema dado es un problema basado en la implementación que se puede resolver siguiendo los pasos a continuación:
- Imprime el primer carácter de la string dada str[0] .
- Almacene la longitud de la string en una variable len e imprima len – 2 .
- Imprime el último carácter de la string, es decir, str[len -1] .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program of the above approach #include <iostream> using namespace std; // Function to convert the given // string into its abbreviation void abbreviateWord(string str) { // Stores the length of the string int len = str.size(); // Print 1st character cout << str[0]; // Print count of characters // in between cout << len - 2; // Print last character cout << str[len - 1]; } // Driver Code int main() { string str = "internationalization"; abbreviateWord(str); return 0; }
Java
// Java program for the above approach import java.util.*; public class GFG { // Function to convert the given // string into its abbreviation static void abbreviateWord(String str) { // Stores the length of the string int len = str.length(); // Print 1st character System.out.print(str.charAt(0)); // Print count of characters // in between System.out.print(len - 2); // Print last character System.out.print(str.charAt(len - 1)); } // Driver Code public static void main(String args[]) { String str = "internationalization"; abbreviateWord(str); } } // This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach # Function to convert the given # string into its abbreviation def abbreviateWord(str): # Stores the length of the string _len = len(str); # Print 1st character print(str[0], end=""); # Print count of characters # in between print(_len - 2, end=""); # Print last character print(str[_len - 1], end=""); # Driver Code str = "internationalization"; abbreviateWord(str); # This code is contributed gfgking
C#
// C# program of the above approach using System; class GFG { // Function to convert the given // string into its abbreviation static void abbreviateWord(string str) { // Stores the length of the string int len = str.Length; // Print 1st character Console.Write(str[0]); // Print count of characters // in between Console.Write(len - 2); // Print last character Console.Write(str[len - 1]); } // Driver Code public static void Main() { string str = "internationalization"; abbreviateWord(str); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to convert the given // string into its abbreviation function abbreviateWord(str) { // Stores the length of the string let len = str.length; // Print 1st character document.write(str[0]); // Print count of characters // in between document.write(len - 2); // Print last character document.write(str[len - 1]); } // Driver Code let str = "internationalization"; abbreviateWord(str); // This code is contributed by Potta Lokesh </script>
Producción
i18n
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)