using System;
class Program
{
static void Main()
{
Console.Write("Enter a string to reverse: ");
string input = Console.ReadLine();
string reversed = ReverseString(input);
Console.WriteLine($"Reversed string: {reversed}");
}
static string ReverseString(string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
Explanation:
ReverseStringMethod:
Converts the string into a character array.
Uses Array.Reverse to reverse the array.
Creates a new string from the reversed character array.
Input and Output:
Takes input from the user.
Outputs the reversed string.
2. How to find if the given string is a palindrome or not?
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
if (IsPalindrome(input))
Console.WriteLine("The string is a palindrome.");
else
Console.WriteLine("The string is not a palindrome.");
}
static bool IsPalindrome(string str)
{
string reversed = ReverseString(str);
return string.Equals(str, reversed, StringComparison.OrdinalIgnoreCase);
}
static string ReverseString(string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
Explanation:
IsPalindromeMethod:
Reverses the input string.
Compares the original string with the reversed one using StringComparison.OrdinalIgnoreCase for case-insensitive comparison.
ReverseStringMethod:
Reverses the input string using a char[]array and Array.Reverse.
Input and Output:
Takes input from the user.
Outputs whether the string is a palindrome or not.
3. How to reverse the order of words in a given string?
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string reversedWords = ReverseWords(input);
Console.WriteLine($"Reversed words: {reversedWords}");
}
static string ReverseWords(string str)
{
string[] words = str.Split(' ');
Array.Reverse(words);
return string.Join(" ", words);
}
}
Explanation:
ReverseWordsMethod:
Splits the string into an array of words usingSplit(' ').
Reverses the order of the words using Array.Reverse.
Joins the reversed array back into a single string using string.Join.
Input and Output:
Takes a string from the user.
Outputs the string with the order of words reversed.
4. How to recount the occurrence of each character in a string?
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
Dictionary <char, int> charCount = CountCharacters(input);
Console.WriteLine("Character occurrences:");
foreach (var pair in charCount)
{
Console.WriteLine($"'{pair.Key}': {pair.Value}");
}
}
static Dictionary <char, int> CountCharacters(string str)
{
var result = new Dictionary<char, int>();
foreach (char c in str)
{
if (result.ContainsKey(c))
result[c]++;
else
result[c] = 1;
}
return result;
}
}
Explanation:
CountCharacters Method:
Loops through each character in the string.
Uses a Dictionary<char, int>to store character counts.
If a character already exists in the dictionary, its count is incremented. Otherwise, it's added with a count of 1.
Input and Output:
Takes a string input from the user.
Displays each character and its corresponding count.
5. How to remove duplicate characters from a string?
using System;
using System.Linq;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string result = RemoveDuplicates(input);
Console.WriteLine($"String without duplicates: {result}");
}
static string RemoveDuplicates(string str)
{
return new string(str.Distinct().ToArray());
}
}
Explanation:
Distinct Method:
The Distinct method from System.Linq removes duplicate characters from the string.
Conversion:
str.Distinct() returns an enumerable collection of unique characters.
ToArray() converts it back to a character array, and new string() forms the result string.
Input and Output:
Takes a string from the user.
Outputs the string with duplicates removed.
6.How to find all possible substring of a given string?
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string or number: ");
string input = Console.ReadLine();
Console.WriteLine("All possible substrings:");
FindSubstrings(input);
}
static void FindSubstrings(string str)
{
for (int i = 0; i < str.Length; i++)
{
for (int j = i + 1; j <= str.Length; j++)
{
Console.WriteLine(str.Substring(i, j - i));
}
}
}
}
Explanation:
Outer Loop:
Iterates through each starting index i of the substring.
Inner Loop:
Iterates through the ending index j, starting from i + 1.
Substring Method:
Extracts a substring from index i with length (j - i).
0 Comments
If you have any queries, please let me know. Thanks.