Write a program that accepts a string input from the user andreverses the conten
ID: 3610345 • Letter: W
Question
Write a program that accepts a string input from the user andreverses the contents of the string. Your program should work byusing two pointers. The “head” pointer should be set tothe address of the first charter in the string and“tail” pointer should set to the address of the lastcharter in the string.[code]
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
char *head, *tail, *cstr;
int i = 0;
cout << "Enter a string: ";
getline(cin, str);
cstr = new char[str.size() + 1];
strcpy_s(cstr,sizeof(cstr), str.c_str());
head = &cstr[0];
tail = &cstr[str.size() - 1];
cout << "String inverted is: ";
while (*head != '') {
cstr[i] = *tail;
*tail--;
*head++;
i++;
}
cout << cstr;
cout <<" ";
return 0;
}
I am getting a linking error stating buffer is too small how to fixthis
Thanks
Explanation / Answer
To fix this, I had to add and subtract a few things. Everything that I subtracted, I have commented out. The onlythings I added were another pointer, an assignment for the newpointer, and 2 for statements. I hope this helps!!! #include #include using namespace std; int main() { string str; char *head, *tail, *cstr, *cstrReverse; int i = 0; 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.