Recurssion issues.

This is a discussion on Recurssion issues. within the C Programming forums, part of the General Programming Boards category; Hi. I've posted here regarding this before, but I moved on and am coming back to it now since I ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Recurssion issues.

    Hi. I've posted here regarding this before, but I moved on and am coming back to it now since I am still having trouble figuring the string permutation using recursion issue.

    The issue I am having is that somewhere before checkpoint 2 there is a segmentation fault. My program just keeps spitting out the same 2 permutations until until it gives the error message. I would love to understand why?

    Code:
    $ ./test.1 -o test.1.c | sort -u
    chechpoit-1: ABCD
    chechpoit-1: CBAD
    checkpoint-2 ABCD
    checkpoint-2 CBAD


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void swap(char *a, char *b){
    char temp;
    
    temp = *a;
    *a = *b;
    *b = temp;
    }
    
    
    
    
    void permute( char *str, int i, int n){
    int j;
    
     if (i == strlen(str))
         printf("%s\n", str);
     else{
    
          for( j = i; j <= strlen(str); j++){
    printf("chechpoit-1: %s\n", str);
    fflush(0);
          swap(str+i,str+n);
    printf("checkpoint-2 %s\n", str);
          permute(str, i, n);
    printf("checkpoint-3 %s\n", str);
          swap(str+i,str+n);}
    }
    }
    
    int main(void){
    
    
    char *ptr; 
    char string[] = "ABCD";
    
    ptr = string;
    
    permute(ptr,0,2);
    return 0;}

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All you ever do is swap 0 and 2 back and forth. Every time your loop runs, you still end up calling it with 0 and 2 and with the same starting point.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging issues
    By melodia in forum C Programming
    Replies: 1
    Last Post: 11-21-2010, 06:56 AM
  2. I hate pointers or Pointer Issues
    By bolivartech in forum C Programming
    Replies: 9
    Last Post: 11-14-2009, 10:48 AM
  3. Better spacing issues
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2008, 03:46 PM
  4. Visual Age C++ to MS VC++ conversion issues?
    By Ruchikar in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2003, 09:54 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 08:01 PM

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