anonymous
  • anonymous
write a c++ program to reverse the string using pointers? please help
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
do you have a char buffer for the result?
anonymous
  • anonymous
do you have to use char arrays or std::string?
anonymous
  • anonymous
OK, here is an in-place solution: #include void reverse(char* s) { const int n = strlen(s); for (int i = 0; i < n / 2; ++i) { char c = s[i]; s[i] = s[n - 1 - i]; s[n - 1 - i] = c; } } int main(int argc, char *argv[]) { char s[] = "hello, world"; reverse(s); std::cout << s << std::endl; return 0; }

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
#include "stdafx.h" const int strLen=6; char szStart[strLen] = "Hello"; char szEnd[strLen] = ""; int _tmain(int argc, _TCHAR* argv[]) { //declare the pointers char* pszStart = szStart; char* pszEnd = szEnd; //make the destination pointer point to the end of the string, //-2 because of the end of string char. Without it we would copy the //string exactly reversed and the end of strign would be the first char //and we would see nothing :D pszEnd += strLen-2; //1) dereference the pointers //2) assign source char to destination char //3) decrease the destination pointer and, //4) increase the source pointer //5) lastly check we are not assigning the end of string char and if so quit while //check we are not at the end of the string while ((*pszEnd-- = *pszStart++) != NULL); return 0; }
anonymous
  • anonymous
Of course this is really a c style app. Todo a c++ version of this really misses the point of c++. It is definitely overkill to use classes for tis type of problem but I suppose it cold be done. Char class, and a string reverser class. c++ is mainly used in larger systems to encapsulate complexity and simplify interfaces between modules.

Looking for something else?

Not the answer you are looking for? Search for more explanations.