Write a function (in your preferred language C, C++, or Java) that will take the given string and return the reverse. (ie. input:"abc def ghi" output:"ihg fed cba") Optimize the above code (if possible). Now knowing that the original function you wrote in pt1 is being used by other programs and cannot be modified write another function that when called will utilize the function from pt1 to only reverse the words in the sentence. (ie. input:"abc def ghi" output:"ghi def abc") Test your code.
Anónimo
string reverseString(string str) { int i = 0; while(i < str.size()/2) { char temp = str[i]; str[i] = str[str.size() - i - 1]; str[str.size() - i - 1] = temp; ++i; } return str; } int main(int argc, char *argv[]) { string temp = "abc def ghi"; cout << temp << endl; cout << reverseString(temp) << endl; } To do the second part, give the algorithm each word, and concatenate them.