Get premium membership and access questions with answers, video lessons as well as revision papers.

Create a function in C++ language called rev_str() that reverses a string.Overload rev_str() so it can be called with either one character array or two.When...

      

Create a function in C++ language called rev_str() that reverses a string.Overload rev_str() so it can be called with either one character array or two.When it is called with one string, have that one string contain the reversal.When it is called with two strings, return the reversed string in second argument.For example
char s1[80], s2[80];
strcpy (s1, "hello");
rev_str(s1, s2; //reversed string goes in s2, s1 untouched
rev_str(s1); //reversed string is returned in s1

  

Answers


Davis
#include
#include
using namespace std;
//Overload string reversal function.
void rev_str (char *s); //reverse string in place
void rev_str (char *in, char *out); //put reversal into out
into main()
{
char s1[80], s2[80];
strcpy (s1, "This is a test");
rev_str(s1, s2);
cout << s2 << "\n";
return 0;
}
//Reverse string, put result in s.
void rev_str (char *s)
{
char temp[80];
int i, j;
for (i=strlen(s)-1, j=0; i>=0; i--, j++)
temp[j] = s[i];
temp [j] = '\0'; // null-terminate result
strcpy (s, temp);
}
// Reverse string, put result into out.
{
int i, j;
for (i=strlen (in) - 1, j=0; i>=0; i--, j++)
out [j] = in[i];
out [j] = '\0'; // null-terminate result
}
Githiari answered the question on May 5, 2018 at 17:36


Next: State five desirable characteristics of information.
Previous: Given the following new-style C++ program, show how to change it into its old-style form.#include using namespace std;into f(into a);into main(){cout << f(10);return 0;}int f(int...

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions