Top 10+ C# Interview Programs for 2024


 


Question:

Write a C# program that takes an input string (e.g., "hello world"), reverses each word in the string, and outputs the result with the words in the original order but with each word reversed.

Example:

  • Input: "hello world"
  • Output: "olleh dlrow"

Code:


using System; class Program { static void Main() { // Input string string input = "hello world"; // Split the string into words string[] words = input.Split(' '); // Loop through each word and reverse it for (int i = 0; i < words.Length; i++) { // Initialize an empty string to build the reversed word string reversedWord = ""; // Loop through each character of the current word in reverse order for (int j = words[i].Length - 1; j >= 0; j--) { // Append each character to the reversedWord string reversedWord += words[i][j]; } // Replace the current word with its reversed version words[i] = reversedWord; } // Join the reversed words back into a single string string output = string.Join(" ", words); // Output the result Console.WriteLine(output); } }

Expected Output:

When you run the program with the input string "hello world", the output will be:


olleh dlrow

Explanation:

Let's break down the code and explain it step by step:

1. Input String:


string input = "hello world";
  • Definition: The input string "hello world" is provided.
  • Explanation: This is the string that will be processed by the program. It contains two words, "hello" and "world", which need to be reversed individually.

2. Split the Input String into Words:


string[] words = input.Split(' ');
  • Definition: This line splits the input string at each space character (' ') into an array of words.
  • Explanation: After splitting the string, we get an array words = ["hello", "world"]. Each word is separated based on the space.

3. Reverse Each Word:


for (int i = 0; i < words.Length; i++) { string reversedWord = ""; for (int j = words[i].Length - 1; j >= 0; j--) { reversedWord += words[i][j]; } words[i] = reversedWord; }
  • Definition: A nested for loop is used to reverse each word in the array.

    • Outer Loop (for (int i = 0; i < words.Length; i++)): Loops through each word in the words array. For words = ["hello", "world"], the loop will run twice — once for "hello" and once for "world".

    • Inner Loop (for (int j = words[i].Length - 1; j >= 0; j--)): This inner loop goes through each character of the word in reverse order. For example:

      • For the word "hello", j will start at 4 (the last character) and go down to 0 (the first character).
      • The characters are added to reversedWord in reverse order, so "hello" becomes "olleh" and "world" becomes "dlrow".
    • Update the word: After the inner loop completes, the reversed word replaces the original word in the words array.

4. Join the Words Back into a Single String:


string output = string.Join(" ", words);
  • Definition: The string.Join(" ", words) method combines the words in the words array back into a single string, with spaces between them.
  • Explanation: After reversing the words, we join them back into a single string. If words = ["olleh", "dlrow"], this step will create "olleh dlrow".

5. Output the Final Result:


Console.WriteLine(output);
  • Definition: This line prints the final result (output) to the console.
  • Explanation: The final output string is "olleh dlrow", which is the input string with each word reversed but the word order remains the same.

Complete Step-by-Step Flow:

  1. Input String: "hello world"
  2. Splitting the String:
    • words = ["hello", "world"]
  3. Reversing Each Word:
    • "hello""olleh"
    • "world""dlrow"
  4. Joining the Reversed Words:
    • output = "olleh dlrow"
  5. Printing the Result:
    • Output: "olleh dlrow"


-------------------------------------------------------------------

Question:

Write a C# program that takes an input string (e.g., "hello jay patel"), reverses each word in the string, and outputs the result with the words in the original order but each word reversed.

Example:

  • Input: "hello jay patel"
  • Output: "olleh yaj letap"

Code:


using System; class Program { static void Main() { // Input string string input = "hello jay patel"; Console.WriteLine("your string is : " + input); // Split the string into words string[] words = input.Split(' '); // Loop through each word and reverse it for (int i = 0; i < words.Length; i++) { // Convert the word to a character array and reverse it char[] wordArray = words[i].ToCharArray(); Array.Reverse(wordArray); // Convert the reversed character array back to a string words[i] = new string(wordArray); } // Join the reversed words back into a single string string output = string.Join(" ", words); // Output the result Console.WriteLine("your reverse string is : " + output); } }

Expected Output:

When you run the program with the input string "hello jay patel", the output will be:


your string is : hello jay patel your reverse string is : olleh yaj letap

Explanation:

Let’s break down the code step by step:

1. Input String:


string input = "hello jay patel"; Console.WriteLine("your string is : " + input);
  • Definition: The input string is initialized as "hello jay patel".
  • Explanation: The Console.WriteLine displays the original string before any modifications. It prints "your string is : hello jay patel".

2. Split the Input String into Words:


string[] words = input.Split(' ');
  • Definition: The Split(' ') method splits the string at each space (' ') into an array of words.
  • Explanation: After splitting, the words array will look like this: ["hello", "jay", "patel"].

3. Reverse Each Word:


for (int i = 0; i < words.Length; i++) { // Convert the word to a character array and reverse it char[] wordArray = words[i].ToCharArray(); Array.Reverse(wordArray); // Convert the reversed character array back to a string words[i] = new string(wordArray); }
  • Definition: This loop iterates through each word in the words array and reverses it.

    • Convert the word to a character array: words[i].ToCharArray() converts each word (e.g., "hello") into an array of characters.

    • Reverse the character array: Array.Reverse(wordArray) reverses the characters in the array. For example, "hello" becomes ['o', 'l', 'l', 'e', 'h'].

    • Convert back to a string: new string(wordArray) converts the reversed character array back into a string. So "hello" becomes "olleh", "jay" becomes "yaj", and "patel" becomes "letap".

  • Explanation:

    • After processing the words, the words array becomes: ["olleh", "yaj", "letap"].

4. Join the Reversed Words Back Into a Single String:


string output = string.Join(" ", words);
  • Definition: The string.Join(" ", words) method joins the reversed words into a single string, with a space (' ') separating each word.
  • Explanation: After the reversal, the words array contains ["olleh", "yaj", "letap"], which will be joined into the string "olleh yaj letap".

5. Output the Final Result:


Console.WriteLine("your reverse string is : " + output);
  • Definition: This line prints the final reversed string to the console.
  • Explanation: It outputs the final result "olleh yaj letap" as "your reverse string is : olleh yaj letap".

Complete Step-by-Step Flow:

  1. Input String: "hello jay patel"

    • Printed: "your string is : hello jay patel"
  2. Step 1: Split the String into Words:

    • words = ["hello", "jay", "patel"]
  3. Step 2: Reverse Each Word:

    • "hello""olleh"
    • "jay""yaj"
    • "patel""letap"
  4. Step 3: Join the Reversed Words:

    • output = "olleh yaj letap"
  5. Step 4: Output the Result:

    • Printed: "your reverse string is : olleh yaj letap"


-----------------------------------------------------------------------

Question:

Write a C# program that takes an input string (e.g., "hello world"), reverses the entire string, and outputs both the original and the reversed string.

Example:

  • Input: "hello world"
  • Output: "hello world", "dlrow olleh"

Code:


using System; using System.Linq; class Program { static void Main() { // Input string string input = "hello world"; // Reverse the string using LINQ string reverseString = new string(input.Reverse().ToArray()); // Output the original and reversed string Console.WriteLine(input); // Prints the original string Console.WriteLine(reverseString); // Prints the reversed string } }

Expected Output:

When the program is run with the input string "hello world", the output will be:


hello world dlrow olleh

Explanation:

Let’s break the code down step by step:

1. Input String:


string input = "hello world";
  • Definition: The input string is initialized to "hello world".
  • Explanation: This is the string that will be processed. It contains two words, "hello" and "world".

2. Reverse the String Using LINQ:


string reverseString = new string(input.Reverse().ToArray());
  • Definition: This line uses LINQ to reverse the entire string.

    • input.Reverse() uses the LINQ method Reverse() to reverse the sequence of characters in the input string. This method returns a reversed sequence of characters (an IEnumerable<char>).

    • .ToArray() converts this reversed sequence of characters into an array of characters.

    • new string() creates a new string from the character array returned by .ToArray().

  • Explanation:

    • "hello world" is first reversed character by character, so it becomes "dlrow olleh".
    • The resulting string "dlrow olleh" is stored in the variable reverseString.

3. Output the Original String:


Console.WriteLine(input); // Prints the original string
  • Definition: This line prints the original string (input) to the console.
  • Explanation: It will print "hello world" as the original string.

4. Output the Reversed String:


Console.WriteLine(reverseString); // Prints the reversed string
  • Definition: This line prints the reversed string (reverseString) to the console.
  • Explanation: It will print "dlrow olleh" as the reversed string.

Step-by-Step Flow:

  1. Input String: "hello world"

  2. Step 1: Reverse the String Using LINQ:

    • input.Reverse() returns the reversed sequence of characters: ['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']
    • .ToArray() converts the sequence into a character array: ['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']
    • new string() constructs the reversed string: "dlrow olleh"
  3. Step 2: Print the Original String:

    • Output: "hello world"
  4. Step 3: Print the Reversed String:

    • Output: "dlrow olleh"

------------------------------------------------------------------------------------------




















































































































































































































0 Comments:

Post a Comment