Csharp Coding - Session



✅ Reverse String Array


using System;

public class Program
{
    public static void Main()
    {
        ReverseStringArray();
    }

    public static void ReverseStringArray()
    {
        string[] str = { "priya", "Sai" };
        int length = str.Length;

        for (int i = 0, j = length - 1; i < j; i++, j--)
        {
            string temp = str[i];
            str[i] = str[j];
            str[j] = temp;
        }

        Console.WriteLine(string.Join(", ", str)); // Output: Sai, priya
    }
}
  
Output: Sai, priya

✅ Reverse String Array + Each Word


using System;

public class Program
{
    public static void Main()
    {
        ReverseStringArray();
    }

    public static void ReverseStringArray()
    {
        string[] str = { "priya", "Sai" };

        // Step 1: Reverse the array
        string[] reversedArray = ReverseArray(str);

        // Step 2: Reverse each word and print
        foreach (string word in reversedArray)
        {
            Console.WriteLine(ReverseWord(word));
        }
    }

    public static string[] ReverseArray(string[] str)
    {
        int length = str.Length;
        for (int i = 0, j = length - 1; i < j; i++, j--)
        {
            string temp = str[i];
            str[i] = str[j];
            str[j] = temp;
        }
        return str;
    }

    public static string ReverseWord(string word)
    {
        char[] chars = word.ToCharArray();
        int left = 0, right = chars.Length - 1;
        while (left < right)
        {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
        return new string(chars);
    }
}      
Output:
iaS
ayirp

✅ Group Words That Are Anagrams


using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string[] input = { "eat", "tan", "aet", "ant" };
        var result = GroupAnagrams(input);

        foreach (var group in result)
        {
            Console.WriteLine(string.Join(", ", group));
        }
    }

    public static List<List<string>> GroupAnagrams(string[] input)
    {
        if (input == null || input.Length == 0)
            return new List<List<string>>();

        var map = new Dictionary<string, List<string>>();

        foreach (var word in input)
        {
            char[] chars = word.ToCharArray();
            Array.Sort(chars);
            string key = new string(chars);

            if (!map.ContainsKey(key))
                map[key] = new List<string>();

            map[key].Add(word);
        }

        return map.Values.ToList();
    }
}
      
Output:
eat, aet
tan, ant

First Non-Repeating Character


string input = "swiss";
var result = input.First(c => input.Count(x => x == c) == 1);
Console.WriteLine(result);
OR

static char FirstNonRepeatingChar(string input)
{
    Dictionary&lt;char, int&gt; charCount = new();
    foreach (char c in input)
        charCount[c] = charCount.ContainsKey(c) ? charCount[c] + 1 : 1;

    foreach (char c in input)
        if (charCount[c] == 1)
            return c;

    return '\0';
}
Output: w

Declare and Invoke a Delegate


public delegate void GreetDelegate(string name);

static void SayHello(string name) =>
    Console.WriteLine($"Hello, {name}!");

GreetDelegate greet = SayHello;
greet("Alice");
      
Output: Hello, Alice!

Simple Event and Subscription


var publisher = new EventPublisher();
var subscriber = new EventSubscriber();

publisher.OnNotify += subscriber.HandleNotification;
publisher.Process();
      
OR

public class EventPublisher
{
    public delegate void NotifyHandler(string message);
    public event NotifyHandler OnNotify;

    public void Process()
    {
        Console.WriteLine("Processing...");
        OnNotify?.Invoke("Processing completed.");
    }
}

public class EventSubscriber
{
    public void HandleNotification(string message) =>
        Console.WriteLine($"Event received: {message}");
}
      
Output:
Processing...
Event received: Processing completed.

Reverse String


Console.WriteLine(ReverseString("hello"));

string ReverseString(string input) =>
    new string(input.Reverse().ToArray());
      
Output: olleh

Palindrome Check


Console.WriteLine(IsPalindrome("madam"));

bool IsPalindrome(string input) =>
    input.Equals(new string(input.Reverse().ToArray()),
                 StringComparison.OrdinalIgnoreCase);
      
Output: True

Remove Duplicates


int[] arr = { 1, 2, 2, 3, 4, 4 };
var result = arr.Distinct().ToArray();
Console.WriteLine(string.Join(", ", result));
      
Output: 1, 2, 3, 4

Find Min and Max


int[] arr = { 10, 25, 3, 7 };
Console.WriteLine($"Min: {arr.Min()}, Max: {arr.Max()}");
      
Output: Min: 3, Max: 25

Bubble Sort


int[] arr = { 5, 3, 8, 1 };
for (int i = 0; i < arr.Length - 1; i++)
    for (int j = 0; j < arr.Length - i - 1; j++)
        if (arr[j] > arr[j + 1])
            (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]);

Console.WriteLine(string.Join(", ", arr));
      
Output: 1, 3, 5, 8

Useful Concepts

Comments

Popular posts from this blog

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Promises in Angular

Comprehensive Guide to C# and .NET Core OOP Concepts and Language Features