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.

Ayushi bhardwaj
  • 441
  • 5
  • 18
  • 2
    This is incredibly confusing for a simple 5 line program. – erip Mar 04 '16 at 19:15
  • 1
    Well, you keep incrementing `str` pointer, and at the end of `rev()` it points into the middle of the array... – Joker_vD Mar 04 '16 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. – n. m. could be an AI Mar 04 '16 at 19:28
  • Possible duplicate of [Reverse String C++ using char array](http://stackoverflow.com/questions/24372480/reverse-string-c-using-char-array) – Han Arantes Mar 04 '16 at 19:29
  • _But I want to know where I'm going wrong with this implementation._ You're making it too hard. – erip Mar 04 '16 at 19:31
  • @n.m. Actually you helped me. Printing in main() function is working fine. But generated one more question. That while printing `str` inside the function why it is not printing the whole string. – Ayushi bhardwaj Mar 04 '16 at 19:33
  • 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? – n. m. could be an AI Mar 04 '16 at 19:41
  • @Ayushibhardwaj The problem is you change the value of `str` in your reversing code and then print it out like it should still point to the beginning of the string, which, of course, it doesn't. – Galik Mar 04 '16 at 19:45
  • @Ayushibhardwaj Tell that to your boss. :) – erip Mar 04 '16 at 19:49
  • @Ayushibhardwaj Clearly you're a student. The linked answer is actually almost the same as mine. I think you're in a weird position to be asking people who are trying to help you to delete their answers... – erip Mar 04 '16 at 20:03
  • Why the down-votes? This is a rare question that actually gives a minimum working example and the desired input/output just like the guidelines ask for. – Galik Mar 04 '16 at 20:11

4 Answers4

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;
  }
}
erip
  • 16,374
  • 11
  • 66
  • 121
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
surajs1n
  • 1,493
  • 6
  • 23
  • 34
  • 1
    @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. – Galik Mar 04 '16 at 19:53
  • 1
    This appears to be the only correct answer to the actual question being asked. – Galik Mar 04 '16 at 20:00
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.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
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;
}
Han Arantes
  • 775
  • 1
  • 7
  • 19