Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Display the Abbreviation of a Text

  • Difficulty Level : Easy
  • Last Updated : 24 Oct, 2021

Given a text, we need to display the Abbreviation of the text. An abbreviation is the shortest form of any word or phrase. It contains a group of letters that takes from the full form of the word or phrase. For example, GeeksforGeeks abbreviation is GFG, or the abbreviation of Advance Data Structure is ADS. 

Examples:

Input : Geeks For Geeks
Output: G.F.G

Input : Data Structures Algorithms
Output: D.S.A

Approach:

To print the abbreviation of a text follow the following steps:

  • Initialize an empty string (i.e. abbr).
  • Append the first letter of string to abbr and also append a  ‘ . ‘  to abbr.
  • Now the String is iterated from left to right if space or tab or a newline character is encountered then append the next letter to the abbr and also append  ‘ . ‘  to the abbr.
  • At the end of the iteration, the abbr will have the abbreviation of the given string.

Example:

C#




// C# program to print the abbreviation of a Text
using System;
  
class GFG{
  
public static string Abbreviation(string inputstr)
{
    string abbr = "";
    int i = 0;
    abbr += inputstr[0];
    abbr += '.';
  
    for(i = 0; i < inputstr.Length - 1; i++)
    {
        if (inputstr[i] == ' ' || inputstr[i] == '\t' || 
            inputstr[i] == '\n')
        {
            abbr += inputstr[i + 1];
            abbr += '.';
        }
    }
    return abbr;
}
  
// Driver code
public static void Main()
{
    string str = "Geeks For Geeks";
    string abr = "";
  
    abr = Abbreviation(str);
  
    Console.Write("The Abbreviation : " + abr);
}
}
Output
The Abbreviation : G.F.G.

Explanation: In this example, we create an Abbreviation() method which returns the abbreviation of the specified string. Or we can say that it returns a string that contains the first letter of a word is added to the abbr string if there is space or tab or a newline character before the word, along with that a ‘ . ‘ is also added. Like in string “Geeks For Geeks” the abbreviation is G.F.G. 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!