Abbreviate given string by replacing all characters with length except the first and last Last Updated : 10 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: i18nExplanation: 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> Outputi18n Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Abbreviate given string by replacing all characters with length except the first and last K kaurareen Follow Improve Article Tags : Strings Mathematical C++ Programs C++ Algo Geek DSA Algo-Geek 2021 +3 More Practice Tags : CPPMathematicalStrings Similar Reads How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read How to Remove Last Character From C++ String? In C++, strings are stored as the std::string class objects. In this article, we will look at how to remove the last character from this C++ string object. For Example, Input: Hello! GeeksOutput: Hello! GeekRemove the Last Character from a String in C++To remove the last character from a string, we 2 min read Concatenate suffixes of a String Given string str the task is to expand the string as follows: If the string is abcd, the resultant string will be d, cd, bcd, and abcd in the concatenated form i.e. dcdbcdabcd. We basically need to concatenate all suffixes. Examples: Input: str = "geeks" Output: sksekseeksgeeks Input str = "water" O 6 min read Reverse Middle X Characters Given a string str and an integer X. The task is to reverse the middle X characters of the given string and then print the modified string. Note that len(str) - X is always even.Examples: Input: str = "geeksforgeeks", X = 3 Output: geeksrofgeeks Middle three character are "geeksforgeeks" Hence the r 9 min read Reverse middle words of a string Given a string str, print reverse all words except the first and last words. Examples: Input : Hi how are you geeks Output : Hi woh era uoy geeks Input : I am fine Output : I ma finePrint the first word.For remaining middle words, print the reverse of every word after reaching end of it. This will p 4 min read Like