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 Assertion = Console.ReadLine(); //Changing the Assertion to lowercase which assist in counting //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 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 for (int i=0;i<Assertion.Size;i++) { if (Assertion[0]==Assertion[i]) { CountCharacter++; } } //Displaying the Rely of every character //Eradicating that character from the string Assertion = } } //Console.ReadLine() to carry the display screen after the execution of Console.ReadLine(); |
Output:
I hope this instance will assist in your interview/examination preparation.
Thanks