Abbreviate given string by replacing all characters with length except the first and last

Given string str, the task is to convert the given string into its abbreviation of the form: first character, number of characters between first and last character, and the last character of the string.
Examples:
Input: str = “internationalization”
Output: i18n
Explanation: First letter ‘i’, followed by number of letters between ‘i’ and ‘n’ i.e. 18, and the last letter ‘n’.Input: str = “zambiatek”
Output: g11s
Approach: The given problem is an implementation based problem that can be solved by following the below steps:
- Print the 1st character of the given string str[0].
- Store the length of the string in a variable len and print len – 2.
- Print the last character of the string i.e, str[len -1].
Below is the implementation of the above approach:
C++
// C++ program of the above approach#include <iostream>using namespace std;// Function to convert the given// string into its abbreviationvoid 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 Codeint main(){ string str = "internationalization"; abbreviateWord(str); return 0;} |
Java
// Java program for the above approachimport 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 abbreviationdef 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 Codestr = "internationalization";abbreviateWord(str);# This code is contributed gfgking |
C#
// C# program of the above approachusing System;class GFG{ // Function to convert the given// string into its abbreviationstatic 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 Codepublic 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> |
Output
i18n
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



