String reverse

Hi all,
I jus wanna print string b after reversing it. but the out put is blank. My code snippet is below. :wall:

int main()
{
  char * a, * b;
  b = new char;
  a = new char;
  int len, le;
  le = 0;
  cout<< " enter your string \n";
  cin>> a;
  len = strlen(a);
  for(int i = len-1; i>=0 ; i--)
  {
    le = len - i;
    b[le]= a;
  }

  cout<< endl << b;
  return 0;
}

It's a while ago for me, but it should be something like:

int main()
{
  char * a, * b;
  b = new char[64];	// allocate enough memory for the variables
  a = new char[64];
  int len, le;
  le = 0;
  cout<< " enter your string \n";
  cin>> a;
  len = strlen(a) - 1;
  
  while(a[le]) {
    b[le]=a[len];
    le++;
    len--;
  }

  b[le] = '\0';		// Terminate the string

  cout<< endl << b;
  return 0;
}

Easy way, using recursion:

#include <stdio.h>
void reverse( char *str )
{
    if ( '\0' == *str )
    {
        return;
    }
    reverse( str + 1 );
    printf( "%c", *str );
    return;
}

int main( int argc, char **argv )
{
    for ( int ii = 0; ii < argc; ii++ )
    {
        reverse( argv[ ii ] );
        printf( "\n" );
    }
}

Ancient version from Knuth:

char *
strrev(char *src)
{
   char *p=src;
   char *q=strchr(src,'\0');
   for(q--; q>p; q--,p++)
   {
      char tmp=*q;
      *q=*p;
      *p=tmp;
   }
   return src;
}

Hi.

From K & R, reverse in-place:

#!/usr/bin/env bash

# @(#) s1	Demonstrate K & R string reversal in-place.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Source:"
cat hi.c

pl " Compile, link, run results:"
rm -f hi
make hi
./hi

exit 0

producing:

% ./s1

-----
 Source:
#include <stdio.h>

int
main ()
{
  char s[] = "Hello, world.";
  void reverse ();
  printf ("%s\n", s);
  reverse (s);
  printf ("%s\n", s);
  return (0);
}

/* K & R, Page 62 */

#include <string.h>

void
reverse (char s[])
{
  char c;
  int i, j;
  for (i = 0, j = strlen (s) - 1; i < j; i++, j--)
    {
      c = s;
      s = s[j];
      s[j] = c;
    }
}

-----
 Compile, link, run results:
cc     hi.c   -o hi
Hello, world.
.dlrow ,olleH

Best wishes ... cheers, drl

Hi :), I'm wondering if this recursive call of reversre ( ... ) wouldn't cause stack overflow?

Note how it limits itself: Every time it calls itself again, it does so on a string one character shorter: reverse(str+1) ...and when it gets a string of zero length, it doesn't call itself at all: f ( '\0' == *str ) return;

So it wouldn't overflow the stack unless you had a string long enough to actually fill the stack with function calls.

#include <iostream>
using namespace std;
int main()
{
        char* str ;
        char revStr[20];
        int i=0;
        cout<<"Enter string to reverse\n";
        gets(str);

        int l = strlen(str);
        str +=l---1;
        while(l >= 0)
        {
                revStr[i++] = *str;
                str--;
                l--;
        }
        revStr = '\0';
        cout<<"Reversed String:: "<<revStr<<"\n";
}

This is the output:

[tethomas@~/C++]./a.out
Enter string to reverse
Hello World
Reversed String:: dlroW olleH

Heavy but funny way : :slight_smile:

# echo "Hello World" | fold -w 1 | tail -r | tr -d '\n' | xargs
dlroW olleH