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

This program must be written in C++ Question Write a function that takes a C str

ID: 3772294 • Letter: T

Question

This program must be written in C++

Question

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 characters and decrement rear to point to the preceding character, and and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length.

Explanation / Answer

#include <iostream>
#include <cstring>
#include <algorithm>

const int LENGTH_MAXIMUM = 200;

void dataSwapping(char b[], int capacity);

int main()
{
std::cout << "Please type letters : ";
char strng[LENGTH_MAXIMUM];
std::cin.getline(strng, LENGTH_MAXIMUM);
int size = strlen(strng);
std::cout << " The size of the string is " << size << " characters. ";
dataSwapping(strng, size);
std::cout << "Swapped string: " << strng << ' ';
}

void dataSwapping(char b[], int size)
{
char *pointerFrnt = b;
char *pointerRear = b + size - 1;
for( ; pointerFrnt<=pointerRear; ++pointerFrnt, --pointerRear)
std::iter_swap(pointerFrnt, pointerRear);
}

/*

sample output:

sh-4.3$ g++ -std=c++11 -o main *.cpp
sh-4.3$ main
Please type letters :   
Hi Chegg user is this fine for you
  
The size of the string is 34 characters.   
Swapped string: uoy rof enif siht si resu ggehC iH   

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote