C# Program to Print the Abbreviation of a Text

This is a C# Program to display the abbreviation of a text.

Problem Description

This C# Program Displays the Abbreviation of a Text.

Problem Solution

Here the string is obtained from the user and the abbreviation of the corresponding string is found and displayed.

Program/Source Code

Here is source code of the C# Program to Display the Abbreviation of a Text. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Display the Abbreviation of a Text
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class abbreviation
{
    string str;
    public void readdata()
    {
        Console.WriteLine("Enter a String :");
        str=Console.In.ReadLine();
    }
    public void abbre()
    {
        char[] c, result;
        int j = 0;
        c = new char[str.Length];
        result = new char[str.Length];
        c = str.ToCharArray();
        result[j++] = (char)((int)c[0] ^ 32);
        result[j++] = '.';
        for (int i = 0; i < str.Length - 1; i++)
        {
            if (c[i] == ' ' || c[i] == '\t' || c[i] == '\n')
            {
                int k = (int)c[i + 1] ^ 32;
                result[j++] = (char)k;
                result[j++] = '.';
            }
        }
            Console.Write("The Abbreviation for {0} is ", str);
            Console.WriteLine(result);
            Console.ReadLine();
 
    }
        public static void Main()
        {
            abbreviation obj=new abbreviation();
            obj.readdata();
            obj.abbre();
        }
}
Program Explanation

In this C# program, we are creating an object ‘obj’ variable to the abbreviation class. In readdata() function we are reading a string using ‘str’ variable. In abbre() procedure we are printing the abbreviation of a text.

advertisement
advertisement

Compute the length of the string present in the ‘str’ variable. For loop is used to print the abbreviation of the text. Initialize the value of ‘i’ variable as 0 and check the condition that the value of ‘i’ variable is less than or equal to the length of the string entered by the user.

If the condition is true then execute the iteration of the loop. If condition statement is used to check the value of c[] variable is equal to empty space or newline using Logical OR Operator, if the condition is true then execute the statement. Print the string obtained from the user and the abbreviation of the corresponding string.

Runtime Test Cases
 
Enter a String : 
meenakshi sundarajan engineering college
The Abbreviation for meenakshi sundarajan  engineering college is M.S.E.C.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.