0

I wrote a program to reverse string where the string is a character array. The program is :

#include<iostream>
void rev(char *str)
{
  char *end;
  end = str;// end will now point to the same address of str
  // now we need to incremet the end pointer upto it encounter null
  if(str)// this checks if the string is present or not
  {
    while(*end)
      ++end;
    --end;// set one character back, since last character is null
    // swap characters from start of string with the end of the string
    // until the pointers meet in middle.
    while(str < end)
    {
      char temp = *str;
      *str++ = *end;
      *end-- = temp;
    }
  }
  std::cout<<"\n The reversed string is : \n";
  std::cout<<str<<std::endl;
}
int main()
{
  char str[500];
  std::cout<<"\n Enter the string : ";
  std::cin.getline(str, sizeof(str));
  rev(str);
  return 0;
}

One output instance is :

Enter the string : asdfgh

 The reversed string is : 
dsa

I want to solve this problem inplace and using character array.

There are many solutions available on internet. I have seen them. But I want to know where I'm going wrong with this implementation. If there is some extra increment in str. I want to know how to correct it.

11
  • 2
    This is incredibly confusing for a simple 5 line program. Commented Mar 4, 2016 at 19:15
  • 1
    Well, you keep incrementing str pointer, and at the end of rev() it points into the middle of the array... Commented Mar 4, 2016 at 19:20
  • 1
    Try some separation of concerns. A function named rev should reverse. Not dance, not play guitar, not bring you coffee and a croissant, not print anything. Reverse. Commented Mar 4, 2016 at 19:28
  • Possible duplicate of Reverse String C++ using char array Commented Mar 4, 2016 at 19:29
  • 1
    We programmers have reduced attention span, that's why we prefer short functions that do one thing. Not so many things to keep track of == good thing. If you insist on doing two things in the same function, you gotta keep track of stuff. For example, when you print str, are you sure it's the whole string? Have you kept track of what you've used str for? Commented Mar 4, 2016 at 19:41

4 Answers 4

5

I think you're making the problem too confusing.

void reverse(char* s) {
  // return if s is nullptr
  if(!s) return;

  size_t len = strlen(s);

  // swap ith from front with ith from back
  for(size_t i = 0; i < len/2; ++i) {
    char temp = s[i];
    s[i] = s[len - i - 1];
    s[len - i - 1] = temp;
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, this is not a helpful answer. Internet is full of such answers.
@Ayushibhardwaj You didn't ask the right question. :)
Nice. I suppose you could have used std::swap, though, no?
The OP is not asking to copy someone else's solution, the question is where the OP's code is going wrong.
Okay I can see that. But I think it was implied that they were asking why their code was not working. The revision clears that up though.
|
4

Well, the error is that you kept on increasing the str pointer and after that decreasing the end pointer, which finally met somewhere at the middle of the string. And right after, you printed out the string which starts somewhere from the middle in reverse order. So, I would suggest to do as follows:

#include<iostream>
void rev(char *str) {
  char *end;
  end = str;

  if(str) {
    while(*end)
        ++end;
    --end;

    while(str < end) {
      char temp = *str;
      *str++ = *end;
      *end-- = temp;
    }
  }
  /* Don't print the string here itself */
}

int main() {
  char str[500];
  std::cout<<"\n Enter the string : ";
  std::cin.getline(str, sizeof(str));
  rev(str);

  /* Print your string here */
  std::cout<<"\n The reversed string is : \n";
  std::cout<<str<<std::endl;
  return 0;
}

OUTPUT:

 Enter the string : qwertyuiop

 The reversed string is : 
poiuytrewq

2 Comments

@erip No, this code works because, by moving the printing outside the function, it prints out a copy of the pointer that is used to reverse the string.
This appears to be the only correct answer to the actual question being asked.
3

Well, since this is tagged c++, you should note that you can do this as

void rev(char* str)
{
    std::reverse(str, str + strlen(str));
}

Looking at the documentation for std::reverse you can deduce that it does it in place.

Comments

0

Try this:

#include<bits/stdc++.h>

int main()
{
    char str[500], revstr[500];
    int i,j,len;
    std::cout<<"\n Enter the string : ";
    std::cin.getline(str, sizeof(str));

    //reverse str
    len = strlen(str);
    for(i=len-1, j=0; i>=0; i--, j++){
        revstr[j]=len[i];
    }
    revstr[j] = '\0'; //marks end of string

    std::cout<<"\n The reversed string is : \n";
    std::cout<<revstr<<std::endl;

  return 0;
}

2 Comments

sorry, but i didn't asked for this.
This solution doesn't fulfill the in-place requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.