Q:

C# program to print the abbreviation of a given string

belongs to collection: C# Basic Programs | String programs

0

C# program to print the abbreviation of a given string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to print the abbreviation of a given string is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to print the abbreviation of a given string.

using System;

class Sample
{
    public static string GetAbbreviation(string str)
    {
        char[] tempArray = new char[str.Length];
        string abbr="";
        int loop = 0;
        
        
        tempArray = str.ToCharArray();

        abbr += (char)((int)tempArray[0] ^ 32);
        abbr += '.';

        for (loop = 0; loop < str.Length - 1; loop++)
        {
            if (tempArray[loop] == ' ' || tempArray[loop] == '\t' || tempArray[loop] == '\n')
            {

                abbr += (char)((int)tempArray[loop + 1] ^ 32);
                abbr += '.';
            }
        }

        return abbr;
    }
    public static void Main()
    {
        string str = "information technology";
        string abr = "";

        abr = GetAbbreviation(str);

        Console.WriteLine("Abbreviation: " + abr);
    }
}

Output:

Abbreviation: I.T.
Press any key to continue . . .

Explanation:

Here, we created a class Sample that contains two static methods GetAbbreviation() and Main().

The GetAbbreviation() method is used to get the abbreviation of a given string. The abbreviation is used to represent the first character of every word in the specified string.

In the Main() method, we created a string str, which is initialized with "information technology" then we get the abbreviation using GetAbbreviation() method, and print the result on the screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | String programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to print the list of all possible subst... >>
<< C# program to convert a string from lowercase to u...