c# Reversing words in a string using single loop without using reverse function and stack -
i have tried writing logic reversing each word in string single loop didn't work. can please provide logic reverse every word in string using single loop , without using reverse function.
input:
welcome world
output:
emoclew ot eht dlrow
my logic 2 loops:
class program { static void main(string[] args) { string input = string.empty; input = console.readline(); string[] strarr=input.split(' '); stringbuilder sb = new stringbuilder(); foreach (string str in strarr) { sb.append(fnreverse(str)); sb.append(' '); } console.writeline(sb); console.read(); } public static string fnreverse(string str) { string result = string.empty; (int = str.length-1; >= 0; i--) result += str[i]; return result; } }
string strin = "welcome world"; string strtmp = ""; string strout = ""; (int i=strin.length-1; i>-1; i--) { if (strin[i] == ' ') { strout = strtmp + " " + strout; strtmp = ""; } else { strtmp += strin[i]; } } strout = strtmp + " " + strout;
gives result "emoclew ot eht dlrow"
Comments
Post a Comment