Abbreviate given String by Replacing all Characters with Length Except the First and Last


In this problem, we need to transform the string of a length greater than 2 into its abbreviation form. We can use the ‘length’ property of the string to count the total number of middle characters in the string, and we can first and last characters using the respected index value.

Problem statement − We have given a string str of length greater than or equal to 2 and need to convert the string into its abbreviation form.

The abbreviation form of the string is as shown here: first character + the total number of middle characters + last character.

Sample examples

Input

str = ‘abcdefg’

Output

a5g

Explanation − The ‘a’ is the first character, ‘g’ is the last character, and total 5 characters are there between them.

Input

 str = ‘tutorialspoint’

Output

t12t

Explanation − Here, the first and last character is ‘t’, and the total number of middle characters is 12.

Input

 str = ‘ab’

Output

a0b

Explanation − Here, the total number of middle characters is 0, so the resultant string is a0b.

Approach 1

In this approach, we will traverse the string to find the length of the string. After that, we will access the first and last character of the string to make its abbreviation.

Algorithm

Step 1 − Define the ‘size’ variable and initialize with 0 to store the total number of characters in the given string.

Step 2 − Use the loop to traverse the string until we get ‘\0’ as a current character.

Step 3 − Print the first character of the string.

Step 4 − Print the size − 2.

Step 5 − Prin the last character of the string using the ‘size − 1’ index.

Example

#include <iostream>
using namespace std;

void abbreviateWord(string alpha){
    int size = 0;
    // counting total characters
    while (alpha[size] != '\0')
        size++;
    // first char
    cout << alpha[0];
    // tota middle characters
    cout << size - 2;
    // last char
    cout << alpha[size - 1];
}
int main(){
    string str = "abcdefghi";
    abbreviateWord(str);
    return 0;
}

Output

a7i

Time complexity− O(N) as we traverse the string to calculate the total number of characters in the string.

Space complexity − O(1) as we don’t use extra space.

Approach 2

In this approach, we will use the size() method of the string in C++ to get the length of the string.

Algorithm

Step 1 − Initialize the ‘len’ variable with the length of the string, which we can get using the size() method.

Step 2 − Access the string's first character using the ‘0’ index, and print it.

Step 3 − Print the ‘len − 2’.

Step 4 − Print the last character of the string.

Example

#include <iostream>
using namespace std;
void abbreviateWord(string alpha) {
    // get the string length
    int len = alpha.size();
    // 1st char
    cout << alpha[0];
    // print len - 2
    cout << len - 2;
    // last char
    cout << alpha[len - 1];
}
int main() {
    string str = "tutorialspoint";
    abbreviateWord(str);
    return 0;
}

Output

t12t

Time complexity− O(N) as we use the size() method.

Space complexity − O(1) as we don’t use dynamic space.

Conclusion

We learned to print the abbreviation of the string using two approaches in this tutorial. In the first approach, we used the loop to calculate the total number of characters in the string, and in the second approach, we used the size() method. However, programmers can also use the length() method.

Updated on: 14-Aug-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements