Sunday, May 19, 2024
HomeC#Program to search out the prevalence of the character in a string...

Program to search out the prevalence of the character in a string in C# ~ IT Tutorials with Instance


In some interviews or exams, an issue is supplied to the interviewee or pupil to search out out the prevalence of the character in a string. On this instance, I’ll aid you in fixing that drawback/program with the best strategy.

Program to search out the prevalence of the character in a string

Let’s observe the method proven within the under picture. As C# within the case-sensitive language, I’m changing the string to the lowercase string in order that I can examine the characters in any other case capital character and small alphabetical character will probably be handled as completely different characters.

Program to search out the prevalence of the character in a string in C# is supplied under:

string Assertion = “”;

Console.WriteLine(“Enter
a string to search out variety of Prevalence of a personality”
);

Assertion = Console.ReadLine();

//Changing the Assertion to lowercase which assist in counting
of the character within the string

//As C# is Case-Delicate

Assertion = Assertion.ToLower();

//Examine the size of string entered by the consumer

if (Assertion.Size>0) {

//Iterate until size of assertion is bigger than zero

//Right here we examine every character prevalence

//As soon as rely is checked, we’ll take away that character from the
string

whereas (Assertion.Size > 0)

{

    //CountCharacter to retailer the character prevalence

    int CountCharacter = 0;

    Console.Write(“Variety of Occurance of ‘”+Assertion[0] + “‘: “);

    //Checking the Character prevalence within the string entered by the
consumer

    for (int i=0;i<Assertion.Size;i++)

    {

        if (Assertion[0]==Assertion[i])

        {

            CountCharacter++;

        }

    }

    //Displaying the Rely of every character

   
Console.Write(CountCharacter+
“n”);

    //Eradicating that character from the string

    Assertion =
Assertion.Substitute(Assertion[0].ToString(),
string.Empty);

}

}

//Console.ReadLine() to carry the display screen after the execution of
this system

Console.ReadLine();

Output:

I hope this instance will assist in your interview/examination preparation.

Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments