Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 10 Jan, 2022

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 = “geeksforgeeks”
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 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>

 
 

Output
i18n

Time Complexity: O(1)
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!