Trusted by millions of Kenyans
Study resources on Kenyaplex

Get ready-made curriculum aligned revision materials

Exam papers, notes, holiday assignments and topical questions – all aligned to the Kenyan curriculum.

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

Answer Attachments

Exams With Marking Schemes

Related Questions