Find Maximum Occurrence of a Character in a Given String

        public static char MaximumOccurringCharacter(this string input)
        {
           
SortedDictionary<char, int> charDict = new SortedDictionary<char, int
>();
           
int
length = input.Length;

            for (int i = 0; i < length; i++)
            {
               
if
(charDict.ContainsKey(input[i]))
                {
                    charDict[input[i]]++;
                }
               
else
               
{
                    charDict.Add(input[i], 1);
                }
            }

            var max = charDict.Values.Max();
           
var
relevantKeys = charDict.Where(pair => max.Equals(pair.Value))
                .Select(pair => pair.Key).First();
           
return
relevantKeys;

        }

Test Case

Console.WriteLine("Max occur char in string test is : " + "test".MaximumOccurringCharacter());

Console.WriteLine("Max occur char in string maximum is : " + "maximum".MaximumOccurringCharacter());

Console.WriteLine("Max occur char in string MaximumOccurringCharacter is : " + "MaximumOccurringCharacter".MaximumOccurringCharacter());

Output

Max occur char in string test is : t
Max occur char in string maximum is : m
Max occur char in string MaximumOccurringCharacter is : r

Reverse the word in a string

For reversing the word in a string I used following algorithm

  1. Traverse the string and push the characters in stack, until we get space
  2. When space is encountered, pop the element from stack and enqueue in queue
  3. enqueue the spaces in queue
  4. once the traverse is complete add the last word in queue

Stack is used to reverse the characters and queue is used to maintain the order of characters

 

   public static void ReverseWordOfString(string s)
    {
      if (string.IsNullOrEmpty(s))
      {
        Console.WriteLine("String is empty");
        return;
      }

      Stack<char> charStack = new Stack<char>();
      var charArray = s.ToCharArray();
      int length = charArray.Length;
      Queue<char> result = new Queue<char>();
      Console.WriteLine("input string : {0}", s);
      for (int i = 0; i < length; i++)
      {
        if (charArray[i] != ' ')
        {
          charStack.Push(charArray[i]);
        }
        else
        {
          while (charStack.Count > 0)
          {
            result.Enqueue(charStack.Pop());
          }
          result.Enqueue(' ');
        }
      }
      while (charStack.Count > 0)
      {
        result.Enqueue(charStack.Pop());

      }

      Console.Write("String After reversing the words : ");
      Console.Write(result.ToArray());
      Console.WriteLine();

    }