Question
Write a function that takes a C string as an input parameterand reverses the string. The function should usetwo pointers front and rear. Thefront pointer should initially reference the firstcharacter in the string, and the rear pointer shouldinitially reference the last character in the string.Reverese thestring by swapping the characters referenced by front andrear, then increment front to point to thenext character and decrement rear to point to thepreceding character, and so on, until the entire string isreversed. Write a main program function to test your function onvarious strings of both even and odd length. Write a function that takes a C string as an input parameterand reverses the string. The function should usetwo pointers front and rear. Thefront pointer should initially reference the firstcharacter in the string, and the rear pointer shouldinitially reference the last character in the string.Reverese thestring by swapping the characters referenced by front andrear, then increment front to point to thenext character and decrement rear to point to thepreceding character, and so on, until the entire string isreversed. Write a main program function to test your function onvarious strings of both even and odd length.
Explanation / Answer
string reverse(string start) { // create pointers intfront=0; intrear=start.length()-1; // swap chars until front & rear pass themiddle while(front<=rear) { // char swap // char swap chartemp=start[front]; start[front]=start[rear]; start[rear]=temp; // increment front & rear front++; rear--; } returnstart; }