GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2007, 03:17
skis0601 skis0601 is offline
New Member
 
Join Date: Dec 2007
Posts: 2
skis0601 is on a distinguished road

Need help reversing an entered String with recursion


Hi there I am trying to write a short program to read in an entered string and then reverse it using recursion. my program is reading it ok but as far as I can tell its not stopping at the end of the string so it never prints the reversed string. Can any1 help out?

CPP / C++ / C Code:
void fib(char &);

int main()
{
  char ch;

  cout << "Enter a String: ";
  fib(ch);

  return EXIT_SUCCESS;
}

void fib(char &ch)
{
  cin.get(ch);
  fib(ch);
  cout << ch;
}
  #2  
Old 03-Dec-2007, 03:29
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: Need help reversing an entered String with recursion


Quote:
Originally Posted by skis0601
Hi there I am trying to write a short program to read in an entered string and then reverse it using recursion. my program is reading it ok but as far as I can tell its not stopping at the end of the string so it never prints the reversed string. Can any1 help out?

CPP / C++ / C Code:
void fib(char &);

int main()
{
  char ch;

  cout << "Enter a String: ";
  fib(ch);

  return EXIT_SUCCESS;
}

void fib(char &ch)
{
  cin.get(ch);
  fib(ch);
  cout << ch;
}

From my point of view, i don't this recursion is necessary. By running a for loop with a swap function is sufficient.

CPP / C++ / C Code:
void swapChar( char* a, char* p )
{
    char temp;

    temp = *a;
    *a = *b;
    *b = temp;
}

The for loop will run half for the string length times and calling the swapChar function. Take note, that swap from the start and the end of the string.

Hope this helps!!






}
  #3  
Old 03-Dec-2007, 03:40
skis0601 skis0601 is offline
New Member
 
Join Date: Dec 2007
Posts: 2
skis0601 is on a distinguished road

Re: Need help reversing an entered String with recursion


unfortunately the purpose of this program is to test recursion so I do need to use it.
  #4  
Old 03-Dec-2007, 06:38
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Need help reversing an entered String with recursion


Just look at what your program is doing.

CPP / C++ / C Code:
void fib(char &ch)
{
  cin.get(ch);        // let's get some input
  fib(ch);              // let's call fib() to get some input and to call fib() to get some input and to call fib() to get some input and to call fib() to get some...
  cout << ch;
}
In other words, there's nothing to break the recursion.

I would do it with something as follows..

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

void reverseString(string& String, int position);

int main()
{
  string String;

  cout << "Enter a String: ";
  getline(cin, String);

  reverseString(String, String.size() - 1);

  return EXIT_SUCCESS;
}

void reverseString(string& String, int position)
{
    cout << String[position];
    if (position > 0)
    {
        reverseString(String, position -1 );
    }
}
__________________
Music, programming, endless learning..
  #5  
Old 03-Dec-2007, 07:58
inevitable inevitable is offline
Junior Member
 
Join Date: Nov 2007
Posts: 53
inevitable is on a distinguished road

Re: Need help reversing an entered String with recursion


Take the below example it will work....
CPP / C++ / C Code:
#include <stdio.h>
 
void revString(char *s, int pos);
 
void main()
{
        char str[100];
        char ch;
        int i=0;
        printf("Enter any string: ");
        do
        {
            scanf("%c",&ch);
            str[i] = ch;
            i++;
        }while(ch!=10);
        str[i]='\0';
        printf("\nOriginal String is %s",str);
        printf("\nString after recursion is : ");
        revString(str,i);
}
 
void revString(char *str,int pos)
{
    if(pos<0)
    {
        return;
    }
    else
    {
        printf("%c",str[pos]);
        revString(str,pos-1);
    }
}
Last edited by admin II : 05-Dec-2007 at 09:19. Reason: Please surround your C code with [cpp] your code [/cpp]
  #6  
Old 03-Dec-2007, 18:41
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: Need help reversing an entered String with recursion


Kimmo has produced a good program here.
__________________
Linux is the best OS in the world.
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple questions for C++ project devster420 CPP / C++ Forum 1 20-Apr-2007 21:26
Message Class TransformedBG CPP / C++ Forum 5 29-Nov-2006 21:28
reverse a string using recursion varun51 C Programming Language 1 13-Oct-2006 18:31
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 00:43
Help wit my source code compiler errors Krandygrl00 CPP / C++ Forum 1 06-Jun-2005 08:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.