1. Check if a String is a Palindrome
Write a method to check if a string is a palindrome.
public static bool IsPalindrome(string input)
{
int left = 0;
int right = input.Length - 1;
while (left < right)
{
if (input[left] != input[right])
return false;
left++;
right--;
}
return true;
}
// Example usage:
bool isPalindrome = IsPalindrome("radar"); // true
bool isPalindrome2 = IsPalindrome("hello"); // false
2. Count the Number of Vowels in a String
Write a method to count the number of vowels in a string.
public static int CountVowels(string input)
{
int count = 0;
string vowels = "aeiouAEIOU";
foreach (char c in input)
{
if (vowels.Contains(c))
count++;
}
return count;
}
// Example usage:
int vowelCount = CountVowels("hello world"); // 3
3.Reverse a String
public static string ReverseString(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
// Example usage:
string reversed = ReverseString("hello"); // "olleh"
4. Reverse Each Word in a String
public static string ReverseEachWord(string input)
{
string[] words = input.Split(' ');
for (int i = 0; i < words.Length; i++)
{
char[] charArray = words[i].ToCharArray();
Array.Reverse(charArray);
words[i] = new string(charArray);
}
return string.Join(" ", words);
}
// Example usage:
string reversedWords = ReverseEachWord("hello world"); // "olleh dlrow"
1. Check if a String is a Palindrome
Write a method to check if a string is a palindrome.
public static bool IsPalindrome(string input) { int left = 0; int right = input.Length - 1; while (left < right) { if (input[left] != input[right]) return false; left++; right--; } return true; } // Example usage: bool isPalindrome = IsPalindrome("radar"); // true bool isPalindrome2 = IsPalindrome("hello"); // false
2. Count the Number of Vowels in a String
Write a method to count the number of vowels in a string.
public static int CountVowels(string input) { int count = 0; string vowels = "aeiouAEIOU"; foreach (char c in input) { if (vowels.Contains(c)) count++; } return count; } // Example usage: int vowelCount = CountVowels("hello world"); // 3
3.Reverse a String
public static string ReverseString(string input) { char[] charArray = input.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } // Example usage: string reversed = ReverseString("hello"); // "olleh"
4. Reverse Each Word in a String
public static string ReverseEachWord(string input) { string[] words = input.Split(' '); for (int i = 0; i < words.Length; i++) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(" ", words); } // Example usage: string reversedWords = ReverseEachWord("hello world"); // "olleh dlrow"
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved