Reverse Words at odd position only C#

Reverse Words at odd position only C#

To reverse words at odd positions in a string in C#, you can use the following approach. This example assumes that words are separated by spaces:

using System; using System.Linq; class Program { static void Main() { string input = "This is a sample sentence to reverse words at odd positions"; string reversed = ReverseWordsAtOddPositions(input); Console.WriteLine(reversed); } static string ReverseWordsAtOddPositions(string input) { string[] words = input.Split(' '); for (int i = 0; i < words.Length; i++) { if (i % 2 != 0) // Check if the position is odd { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } } return string.Join(" ", words); } } 

In this example:

  • The ReverseWordsAtOddPositions function splits the input string into an array of words using Split(' ').
  • It then iterates through the words and checks if the position is odd.
  • If the position is odd, it reverses the characters of the word using Array.Reverse.
  • Finally, it joins the words back into a string using string.Join.

The output of this example would be:

This si a elpmas sentence ot esrever words ta odd snoititlpos 

Examples

  1. Reverse words at odd positions in a string in C#:

    • "C# reverse words at odd positions"
    • Description: Reverse words at odd positions (1-indexed) in a given string.
    // Code Implementation string input = "Reverse words at odd positions in C#"; string result = ReverseWordsAtOddPositions(input); 
    // Function Implementation static string ReverseWordsAtOddPositions(string input) { string[] words = input.Split(' '); for (int i = 0; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(" ", words); } 
  2. Reverse words at odd indices in a sentence in C#:

    • "C# reverse words at odd indices"
    • Description: Reverse words at odd indices (0-indexed) in a given sentence.
    // Code Implementation string sentence = "Reverse words at odd indices in C#"; string result = ReverseWordsAtOddIndices(sentence); 
    // Function Implementation static string ReverseWordsAtOddIndices(string sentence) { string[] words = sentence.Split(' '); for (int i = 1; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(" ", words); } 
  3. Reverse words at odd positions with punctuation in C#:

    • "C# reverse words at odd positions with punctuation"
    • Description: Reverse words at odd positions, considering punctuation in the input string.
    // Code Implementation string input = "Reverse, words at odd positions in C#!"; string result = ReverseWordsAtOddPositionsWithPunctuation(input); 
    // Function Implementation static string ReverseWordsAtOddPositionsWithPunctuation(string input) { string[] words = Regex.Split(input, @"(\W+)"); for (int i = 0; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Concat(words); } 
  4. Reverse words at odd positions in a C# string with LINQ:

    • "C# reverse words at odd positions LINQ"
    • Description: Utilize LINQ to reverse words at odd positions in a given string.
    // Code Implementation string input = "Reverse words at odd positions in C#"; string result = ReverseWordsAtOddPositionsWithLINQ(input); 
    // Function Implementation static string ReverseWordsAtOddPositionsWithLINQ(string input) { string[] words = input.Split(' '); return string.Join(" ", words.Select((word, index) => index % 2 == 0 ? word : new string(word.Reverse().ToArray()))); } 
  5. Reverse words at odd indices in a C# string using StringBuilder:

    • "C# reverse words at odd indices StringBuilder"
    • Description: Use StringBuilder to efficiently reverse words at odd indices in a given string.
    // Code Implementation string sentence = "Reverse words at odd indices in C#"; string result = ReverseWordsAtOddIndicesWithStringBuilder(sentence); 
    // Function Implementation static string ReverseWordsAtOddIndicesWithStringBuilder(string sentence) { string[] words = sentence.Split(' '); StringBuilder result = new StringBuilder(); for (int i = 1; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); result.Append(new string(charArray)).Append(' '); } return result.ToString().Trim(); } 
  6. Reverse words at odd positions with case sensitivity in C#:

    • "C# reverse words at odd positions case sensitivity"
    • Description: Reverse words at odd positions while considering case sensitivity in the input string.
    // Code Implementation string input = "Reverse Words at Odd Positions in C#"; string result = ReverseWordsAtOddPositionsWithCaseSensitivity(input); 
    // Function Implementation static string ReverseWordsAtOddPositionsWithCaseSensitivity(string input) { string[] words = input.Split(' '); for (int i = 0; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(" ", words); } 
  7. Reverse words at odd positions without using additional arrays in C#:

    • "C# reverse words at odd positions without additional arrays"
    • Description: Reverse words at odd positions without using additional arrays, directly modifying the input string.
    // Code Implementation string input = "Reverse words at odd positions in C#"; ReverseWordsAtOddPositionsWithoutArrays(ref input); 
    // Function Implementation static void ReverseWordsAtOddPositionsWithoutArrays(ref string input) { char[] chars = input.ToCharArray(); int wordStart = 0; for (int i = 0; i < chars.Length; i++) { if (chars[i] == ' ') { if (wordStart % 4 < 2) { Array.Reverse(chars, wordStart, i - wordStart); } wordStart = i + 1; } } if (wordStart % 4 < 2) { Array.Reverse(chars, wordStart, chars.Length - wordStart); } input = new string(chars); } 
  8. Reverse words at odd positions while preserving leading/trailing spaces:

    • "C# reverse words at odd positions with leading/trailing spaces"
    • Description: Reverse words at odd positions while preserving leading/trailing spaces in the input string.
    // Code Implementation string input = " Reverse words at odd positions in C# "; string result = ReverseWordsAtOddPositionsWithSpaces(input); 
    // Function Implementation static string ReverseWordsAtOddPositionsWithSpaces(string input) { string[] words = input.Split(' '); for (int i = 0; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(" ", words); } 
  9. Reverse words at odd positions in a C# sentence with regex:

    • "C# reverse words at odd positions regex"
    • Description: Use regex to identify and reverse words at odd positions in a given sentence.
    // Code Implementation string sentence = "Reverse words at odd positions in C#"; string result = ReverseWordsAtOddPositionsWithRegex(sentence); 
    // Function Implementation static string ReverseWordsAtOddPositionsWithRegex(string sentence) { string[] words = Regex.Split(sentence, @"\s+"); for (int i = 0; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(" ", words); } 
  10. Reverse words at odd positions with custom word separator in C#:

    • "C# reverse words at odd positions custom separator"
    • Description: Reverse words at odd positions in a given string using a custom word separator.
    // Code Implementation string input = "Reverse-words-at-odd-positions-in-C#"; string result = ReverseWordsAtOddPositionsWithCustomSeparator(input, '-'); 
    // Function Implementation static string ReverseWordsAtOddPositionsWithCustomSeparator(string input, char separator) { string[] words = input.Split(separator); for (int i = 0; i < words.Length; i += 2) { char[] charArray = words[i].ToCharArray(); Array.Reverse(charArray); words[i] = new string(charArray); } return string.Join(separator.ToString(), words); } 

More Tags

on-screen-keyboard relational-algebra xor spinnaker named-pipes buefy connection-string fadeout vue-directives microsoft-dynamics

More Programming Questions

More Entertainment Anecdotes Calculators

More Housing Building Calculators

More Various Measurements Units Calculators

More Everyday Utility Calculators