This is the question Write a function that takes two pointers to char and copies
ID: 3638973 • Letter: T
Question
This is the questionWrite a function that takes two pointers to char and copies the content of one array in reverse order into the other and returns the number of charaters in data1.
The function should be defined as follows,
int reverse (char *data1, char *data2)
Since the array length is not passed into the function it should iterate (cycle) through the values till it sees a NULL ‘’ character.
This is the pseudo code
Can somebody help me please I do not understand pointers and please put comments
Thank you for your time
#include <iostream>
using namespace std;
/*
Description: copies characters from one string into the other
in reverse order
Parameters:
data1: pointer to source string
data2: pointer to destination string
Return Value: Number of characters copied
*/
int reverse(char* data1, char *data2)
{
return 0;
}
int main()
{
char data1[100];
char data2[100];
*data2 = ''; // Force the string to be empty
cout << "Enter text: " << endl;
cin >> data1;
int length = reverse(data1, data2);
cout << "Original String: " << data1 << endl;
cout << "Reverse String: " << data2 << endl;
cout << "String Length: " << length << endl;
char key;
cin >> key;
return 0;
}
Explanation / Answer
#include using namespace std; /* Description: copies characters from one string into the other in reverse order Parameters: data1: pointer to source string data2: pointer to destination string Return Value: Number of characters copied */ int reverse(char* data1, char *data2) { int i=0, t=0; while(data1[i]!='') {i++;} //count the number of letters for(int k=i-1;k>=0;k--) //do the reverse data2[t++]=data1[k]; return i; } int main() { char data1[101]; char data2[100]={''}; //force the string to be empty coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.