A collection of C# Coding Challenges
Problem:
Write a program to calculate the frequency of each character in a given string (case-insensitive) and display the results in alphabetical order. Ignore spaces and special characters.
Requirements:
Example Input:
"Hello, World!"
Expected Output:
D: 1 E: 1 H: 1 L: 3 O: 2 R: 1 W: 1
Objective:
Write a C# program that reverses the words in a sentence while maintaining the original word order and sentence structure.
Input:
"Hello World! How are you?"
Output:
"you? are How World! Hello"
Requirements:
Considerations:
Example Test Cases:
Input: "Coding is fun!"
Output: "fun! is Coding"
Problem:
Write a C# program that takes a sentence as input and identifies all the words that are palindromes.
A palindrome is a word that reads the same backward as forward (e.g., "radar", "level").
Requirements:
Example Input:
Enter a sentence: "Madam Arora teaches malayalam and a radar is nearby."
Expected Output:
Palindromes found: Madam, Arora, malayalam, radar
Problem:
Write a C# program that takes two strings as input and checks if they are anagrams of each other.
Definition of an Anagram: Two strings are anagrams if they contain the same characters in the same frequency, but possibly in a different order.
Requirements:
Example Input:
String 1: "Listen" String 2: "Silent"
Expected Output:
The strings are anagrams.
Problem:
Write a C# program that counts the number of vowels and consonants in a given string. Ignore spaces, punctuation, and other non-alphabetic characters. Your program should print the total count of vowels and consonants separately.
Input:
A sentence entered by the user.
Example Input: "Hello, world!"
Output:
Total Vowels: 3 Total Consonants: 7
Requirements:
foreach
loop to iterate through each character.Problem:
Write a C# program that prints a number pyramid with a given number of rows. The pyramid should be symmetrical and made of numbers.
Input:
The user provides the number of rows for the pyramid.
Example Input: 5
Output:
1 121 12321 1234321 123454321
Requirements:
Problem:
You are given an array of distinct numbers taken from a range. The array contains numbers from a
to b
, but one number is missing. Your task is to find the missing number in this range. The range can start from any number, not necessarily 1.
Approach:
We can use the sum formula for an arithmetic sequence to efficiently find the missing number. The sum of numbers from a
to b
is given by the formula:
Sum(a, b) = (b - a + 1) × (a + b) / 2
Steps:
a
to b
using the formula.Example:
Given the array: [3, 4, 5, 7, 8] Range: 3 to 8 Missing number: 6 1. Calculate the expected sum: Sum(3, 8) = (8 - 3 + 1) × (3 + 8) / 2 = 6 × 11 / 2 = 33 2. Calculate the actual sum of the array: 3 + 4 + 5 + 7 + 8 = 27 3. The missing number: Missing = 33 - 27 = 6
Requirements:
a
to b
).Problem:
You are given a sequence of integers that should form a continuous sequence without gaps. However, some numbers may be missing from the sequence. Your task is to identify and display all the missing numbers.
Requirements:
Steps:
Example:
Input: 1, 2, 4, 5, 7, 9 Output: The missing numbers are: 3, 6, 8 Input: 1, 2, 3, 4, 5 Output: No missing numbers Input: 18, 12, 13, 15, 16 Output: The missing numbers are: 11, 14
Problem:
Write a program to find the most frequently occurring character in a given string. If there is a tie (two or more characters appear the same maximum number of times), return any one of them.
Requirements:
Steps:
Example:
Input: "characteristics" Output: The most frequent character is: 'c'
Problem:
Write a program that analyzes a given string and performs the following:
1. Character Frequency Analysis:
Identify the top 3 most frequently occurring characters (case-insensitive). If two characters have the same frequency, prioritize lexicographically smaller characters (e.g., 'a' over 'b').
2. Longest Consecutive Sequence:
Find the longest consecutive sequence of any character (e.g., "aaabbcccaaa" has a sequence of "aaa" with a length of 3).
3. Generate a New String:
Construct a new string that contains:
Requirements:
Steps:
Example:
Input: "aaabbcccaaadddeefff" Output: Top 3 characters: a: 6 c: 3 f: 3 Longest consecutive sequence: aaa (length: 3) Generated string: aaaaaacccfffaaa