Recursion

This is a discussion on Recursion within the C++ Programming forums, part of the General Programming Boards category; Code: char func(int , int ,char *, char *); int main (void) { char str[MAX]; char *temp; int y=0, counter=0; ...

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Question Recursion

    Code:
    char func(int , int ,char *, char *);
    
    int main (void)
    {
     char str[MAX];
        char *temp;
        int y=0, counter=0;
    
        cout << "Enter String: ";
        fgets(str, MAX, stdin);
        temp = new char [strlen(str)];                
    
    	*temp = UselessFunction(counter,y, temp, str);
    
    	strcpy(str,temp);                   
    	strrev(temp);                        
    
    	if(strcmp(str,temp)==0)              
    		cout << "\n\n Palindrome\n";
    	else
    		cout << "\nnot a Palindrome\n";
    	
    return 0;
    }
    
    char func(int counter, int y, char* temp, char* str)
    {
    	
    	if (!isspace(str[counter]))         //removing spaces
    	{
    		temp[y] = str[counter];
    		y++;
    	}
    	if (counter < strlen(str)-1)
    		UselessFunction(++counter,y, temp, str);
    	return *temp;
    }
    returning garbage
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    ok i think this line is giving me problem:
    Code:
    temp = new char [strlen(str)];
    gives me wrong length


    but i dont see why?
    Last edited by dayknight; 01-25-2006 at 06:03 PM.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    umm nm, i was placing the null at wrong place
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I'm going to guess:
    Code:
    temp = new char [strlen(str)];
    ->
    Code:
    temp = new char [strlen(str)+1];
    And don't forget <cstring> and <cctype>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > *temp = UselessFunction(counter,y, temp, str);
    Is there a reason why you overwrite the first char of the result string, when you return from the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 08:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21