Hello, I need help with an assignment: There is no user input for this program.
ID: 3852139 • Letter: H
Question
Hello, I need help with an assignment:
There is no user input for this program.
Create a char pointer. Initialize the pointer to point to "John Doe".
Print the name using the pointer.
Print the string in reverse order. That is print the last letter first, the second to the last letter next, and so on. You must use the char pointer for everything. You cannot use an index (subscript) anywhere in your program. That means you cannot have any [ ] in your program (no square brackets). Your code must work for any string not just "John Doe". Meaning if I change only your string from step 2 to "Samuel Smith", with no other modifications, your code should be able to print Samuel Smith backwards.
Print the string again as you did in step 3 (you will lose points for not printing the string again).
Do not use the CString class. Do not use the string class. Make sure all the code to reverse the string is your own original code. Do not use code or a routine authored by someone else even if that routine came with your C++ compiler. You can use the strlen function located in the cstring header file.
The final product should look like this
Explanation / Answer
the c++ programming code is
#include <iostream.h>
#include<string>
using namespace std;
void reversepointerdata(char*);
int main()
{
char *data = "John Doe";
cout << "Name is: " << data << endl;
cout << "Name backwards is: ";
reversepointerdata(data);
cout << endl;
}
//method to reverse the pointer data
void reversepointerdata(char *in)
{
int len = strlen(in);
for (int i= (len - 1); i > -1; i--)
{
char chr = *(in+i);
cout << chr;
}
}
the output is:Name is: John Doe
Name backwards is: eoD nhoJ
the output when we enter Samuel Smith is htimS leumaS.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.