C# Copy substring of a string then put it before the next word of the string -
i trying re-create lastly character of every word of string set copied character first position of next word. example, if come in string "the quick brownish fox jumps on lazy dog", output should "the equick kbrown nfox xjumps sover rthe elazy ydog";
here have far:
string s = "the quick brownish fox jumps on lazy dog"; (int = 0; < s.length; a++) { string b = s.substring(a,1); if (b == " ") { string c = s.substring(a - 1, 1); string d = s.insert (a+1, c); console.write(d); } }
the result of this : equick brownish fox jumps on lazy dogthe quick kbrown fox jumps on lazy dogthe quick brownish nfox jumps on lazy dogthe quick brownish fox xjumps o ver lazy dogthe quick brownish fox jumps sover lazy dogthe quick brownish fox jumps on rthe lazy dogthe quick brownish fox jumps on elazy dogthe quick br own fox jumps on lazy ydog
what trying accomplish output "the equick kbrown nfox xjumps sover rthe elazy ydog"
thanks answered way :)
what this?
string s = "the quick brownish fox jumps on lazy dog"; char lastchar = default(char); bool addlastchar = false; var stringbuilder = new stringbuilder(s); (int = 0; < stringbuilder.length; i++) { var ch = stringbuilder[i]; // todo: consider using char.iswhitespace(ch) method call. // please note: homecoming true different whitespace characters (tabulation, line feed, carriage return, etc). if (ch == ' ') { addlastchar = true; } else { if (addlastchar) { stringbuilder.insert(i, lastchar); addlastchar = false; } lastchar = ch; } } var result = stringbuilder.tostring();
the code works fine strings more 1 space in middle, this:
string s = "the quick brownish fox jumps on lazy dog";
c#
No comments:
Post a Comment