A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

write a c++ program to reverse the string using pointers? please help

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    do you have a char buffer for the result?

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    do you have to use char arrays or std::string?

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    OK, here is an in-place solution: #include <iostream> 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; }

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    #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; }

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.