
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Alphanumeric Abbreviations of a String in C Program?
Here we will see one interesting problem related to alphanumeric abbreviation of a given string. The string length is less than 10. We will print all alphanumeric abbreviations.
The alphanumeric abbreviation is in the form of characters mixed with the digits. The value of that digit is number of characters that are missed. There may be any number of skipped substrings. No two substrings are adjacent to each other. Let us see the algorithm to get the idea.
Algorithm
printAbbreviation(s, index, max, str) −
begin if index is same as max, then print str end if add s[index] at the last of str printAbbreviation(s, index + 1, max, str) delete last character from str count := 1 if str is not empty, then if the last character of str is a digit, then add last digit with the count value delete last character from str end if end if add count after the str printAbbreveation(s, index + 1, max, str) end
Example
#include <iostream> using namespace std; void printAbbreviation(const string& s, int index, int max_index, string str) { if (index == max_index) { //if string has ended cout << str << endl; return; } str.push_back(s[index]); // push the current character to result printAbbreviation(s, index + 1, max_index, str); //print from next index str.pop_back(); //remove last character int count = 1; if (!str.empty()) { if (isdigit(str.back())) { //if the last one is digit, then count += (int)(str.back() - '0'); //count the integer value of that digit str.pop_back(); //remove last character } } char to_char = (char)(count + '0'); //make count to character str.push_back(to_char); printAbbreviation(s, index + 1, max_index, str); //do for next index } void printCombination(string str) { if (!str.length()) //if the string is empty return; string str_res; printAbbreviation(str, 0, str.length(), str_res); } int main() { string str = "HELLO"; printCombination(str); }
Output
HELLO HELL1 HEL1O HEL2 HE1LO HE1L1 HE2O HE3 H1LLO H1LL1 H1L1O H1L2 H2LO H2L1 H3O H4 1ELLO 1ELL1 1EL1O 1EL2 1E1LO 1E1L1 1E2O 1E3 2LLO 2LL1 2L1O 2L2 3LO 3L1 4O 5
- Related Articles
- Program to find whether a string is alphanumeric.
- PHP program to remove non-alphanumeric characters from string
- Python Program to accept string ending with alphanumeric character
- Golang Program to Check if the String is Alphanumeric
- Generate random alphanumeric string in Swift
- Program to find sum of digits that are inside one alphanumeric string in Python
- How to check if a string is alphanumeric in Python?
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Sort only numbers from alphanumeric string in MySQL?
- Python - Check If All the Characters in a String Are Alphanumeric?
- How to remove all non-alphanumeric characters from a string in MySQL?
- What is the Python regular expression to check if a string is alphanumeric?
- C# program to count occurrences of a word in string
- Write a palindrome program in JavaScript so that only alphanumeric values should be allowed?
- C# Program to count vowels in a string

Advertisements