
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to reverse a given string word by word instead of letters using C#?
Create a method reverse Words that takes char array as an input and for each and every character until the empty space is not reached reverse the word. At the last step reverse the entire string from length 0 to n-1 length. In the first step the string “This is my book” will be turned into “koob ym si siht”. At the end of the second step the string words will be reversed to “book my is This”
Time complexity − O(N)
Example
using System; namespace ConsoleApplication{ public class Arrays{ static void reverse(char[] str, int start, int end){ char temp; while (start <= end){ temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } public char[] reverseWords(char[] s){ int start = 0; for (int end = 0; end < s.Length; end++){ if (s[end] == ' '){ reverse(s, start, end); start = end + 1; } } reverse(s, 0, s.Length - 1); return s; } } class Program{ static void Main(string[] args){ Arrays a = new Arrays(); string s = " This is my book "; var res = a.reverseWords(s.ToCharArray()); Console.WriteLine(new String(res)); Console.ReadLine(); } } }
Output
book my is This
- Related Articles
- Program to reverse the position of each word of a given string in Python
- Remove a Given Word from a String using C++
- Write a java program to reverse each word in string?
- C program to Replace a word in a text by another given word
- Write a java program reverse tOGGLE each word in the string?
- How to replace all occurrences of a word in a string with another word in java?
- C++ program to read file word by word?
- Java program to reverse each word in a sentence
- Python program to reverse each word in a sentence?
- Find largest word in dictionary by deleting some characters of given string in C++
- Program to find length of longest word that can be formed from given letters in python
- How to Find Line Number of a Given Word in text file using Python?
- How to capitalize the first letter of each word in a string using JavaScript?
- Check if a string starts with given word in PHP
- Check if a string ends with given word in PHP

Advertisements