Friday, 26 January 2018

Reverse words in a string without using in-built function

b) string str = "I am Ashish Verma";

out- I ma hsihsA  amreV

Exp -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringReverse
{
    class Program
    {
        
        static public string ReverseWordsInString(string stringTobeReverse)
        {
            string reverse = string.Empty;
           
            string[] strArray = stringTobeReverse.Split(' ');

            foreach (var item in strArray)
            {
                string subReverse = string.Empty;
                for (int i = item.Length-1; i >= 0; i--)
                {
                    subReverse += item[i];
                }
                reverse += subReverse + " ";
            }
            return reverse;
        }

        static void Main(string[] args)
        {
           string str = "I am Ashish Verma";

           string s1 = ReverseWordsInString(str);
           Console.WriteLine(s1);  //output  I ma hsihsA amreV
           Console.ReadLine();
        }
    }

}

No comments:

Post a Comment