Reverse a string

// Online C# Editor for free // Write, Edit and Run your C# code using C# Online Compiler using System; public class HelloWorld { public static void Main(string[] args) { var x= "hi sahir"; string reverse =""; for (int i= x.Length-1;i>=0;i--) { reverse = reverse + x[i]; } Console.WriteLine(reverse); Console.ReadLine(); } } public static string Reverse1(string Input) { // Converting string to character array char[] charArray = Input.ToCharArray(); // Declaring an empty string string reversedString = ""; // Iterating the each character from right to left for (int i = charArray.Length - 1; i >=0; i--) { // Append each character to the reversedstring. reversedString = reversedString+ charArray[i]; } // Return the reversed string. return reversedString; ...

151. Reverse Words in a String

151. Reverse Words in a String Leetcode Qn : Qn View public class Solution { public string ReverseWords(string str) { // Split the input string by whitespace into an array of words string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Reverse the array of words Array.Reverse(words); // Join the reversed words back into a single string string reversedString = string.Join(" ", words); return reversedString; } } Demo Demo in sepearte

28. Find the Index of the First Occurrence in a String

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Leetcode Qn : Demo public class Solution { public int StrStr(string haystack, string needle) { string mainString = haystack; string searchString = needle; int indexOfFirstOccurrence = mainString.IndexOf(searchString); if (indexOfFirstOccurrence != -1) { Console.WriteLine($"The first occurrence of '{searchString}' is at index: {indexOfFirstOccurrence}"); return indexOfFirstOccurrence; } else { Console.WriteLine($"'{searchString}' not founds in the string."); } return indexOfFirstOccurrence; } } Demo Demo in sepearte