Write code to print the words in reverse in given string without using any inbuilt functions. Not the entire string, only words in the given string should reverse.
static void Main(string[] args) { string text = "This is Mbox tuts"; //Input string is "This is Mbox tuts" Console.WriteLine(PrintWordsInReverse(text)); //output: "sihT si xobM stut" } // This method will print the words in reverse in given string static string PrintWordsInReverse(string str) { string revWord = string.Empty; string finalSentance = string.Empty; for (int i = 0; i < str.Length; i++) { if (str[i] != ' ') { revWord = str[i] + revWord; } else { revWord = revWord + ' '; finalSentance = finalSentance + revWord; revWord = string.Empty; } } if (revWord != string.Empty) finalSentance = finalSentance + revWord; return final
0 comments:
Post a Comment