Reverse the individual words in a string maintaining case sensitivity and white space. (char array). input[] = "Apple Banana Cherry" output[] = "elppA ananaB yrrehC"
Anonymous
#include #include using namespace std; void revstr(string &s, string &buf) { string delims = " \t"; buf = ""; int wsp; int i, j; for (i = 0; i < s.length(); i = wsp + 1) { wsp = s.find_first_of(delims, i); if (wsp == string::npos) { wsp = s.length(); } for (j = i; j < wsp; j++) { buf.append(1,s[wsp - (j-i) - 1]); } buf.append(1, s[wsp]); } } int main() { string str = "Apple Banana Cherry"; string buf; cout << "input = " << str << endl; revstr(str, buf); cout << "output = " << buf << endl; return 0; }
Check out your Company Bowl for anonymous work chats.