Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help with pointers c++ Write a function that takes a C-string as an input parame

ID: 3779299 • Letter: H

Question

help with pointers c++

Write a function that takes a C-string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed.

Make sure to test your program on various strings of both even and odd length.

The Functions (in separate files):

The only function will be called reverse. This function must use two pointers, and perform the reverse process entirely with those pointers (front and back).

use this code for main. Very simply, your output must display correctly using these 5 test cases.

int main()

{

// these are the test cases

char str1[] = "time";

char str2[] = "straw";

char str3[] = "deliver";

char str4[] = "star";

char str5[] = "knits";

cout << "The strings before reversing: " << endl;

cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

reverse(str1);

reverse(str2);

reverse(str3);

reverse(str4);

reverse(str5);

  cout << "The strings after reversing: " << endl;

cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

return 0;

}

Explanation / Answer

#include <iostream>
#include <cstring>
using namespace std;
/* method reverse for reversing the string */
void reverse(char *str)
{
/* two pointers where front pointing to the strting of the string and
back pointing to the last character of the string */
char *front = str, *back = str + strlen(str) - 1;
/* while front is less than back */
while (front < back) {
/* swapping of front with back,increase index of front
and decrease index of back */
char temp = *front;
*front++ = *back;
*back-- = temp;
}
}
int main()
{
// these are the test cases
char str1[] = "time";
char str2[] = "straw";
char str3[] = "deliver";
char str4[] = "star";
char str5[] = "knits";


cout << "The strings before reversing: " << endl;
cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

reverse(str1);
reverse(str2);
reverse(str3);
reverse(str4);
reverse(str5);

cout << "The strings after reversing: " << endl;
cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

return 0;
}

/****OUTPUT******
The strings before reversing:   
time straw deliver star knits   
The strings after reversing:
emit warts reviled rats stink
*****OUTPUT******/

/* Note:Code has been tested on g++ compiler,Please ask in case of any doubt,Thanks,God bless you */