Thread: Recursive function and segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    Recursive function and segmentation fault

    Hello all,

    I am new to C programming and I am currently working an encoder program that encodes text unintelligibly. I need help writing my recursive function to provide the desired output results. A sample is below. Can anybody help me out or point me in the right direction or even tell me what I'm doing wrong?

    Also note this program was written using Netbeans 6.9.1 with the Cygwin compiler. When I run my program on the school server, 'which is probably an older compiler', I get a segmentation fault.

    What its supposed to look like:
    Please enter a line of text. Enter a new line to complete entry.
    Hello how are you

    The encoded text is:

    ouyed reaed owhed elloHed


    My output results:
    Please enter a line of test. Enter new line to complete.

    Hello how are you
    word count:0 elloedH
    word count:1 owedh
    word count:2 reeda
    word count:3 ouedy
    ouedy reeda owedh elloedH


    This is a rough draft of my code.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define LEN_SENT 100
    #define LEN_WORD 90
    #define NUM_WORDS 50
    #define REV_WORDS 40
    
    /* function prototypes */
    void encode( char *, char * );
    
    int main()
    {
        /* variable and array declarations */
        char string[ LEN_SENT ];
        char *x;
        char word[NUM_WORDS][ LEN_WORD ];
        int i, j;
    
        printf( "Please enter a line of test. Enter new line to complete.\n\n" );
    
        fgets(string, LEN_SENT, stdin); /* read the line of text */
    
        //printf("%s\n", string);
    
        i = 0;
        x = strtok(string, " -\n");
        while (x != NULL) {
           encode ( word[i], x );
           printf ("word count:%d %s\n", i++, word[i]);
           x = strtok(NULL, "-\n ");
        }
        for (j = i-1;j > 0; j-- ){
        //for (j = 0;j < i; j++ ){
        	printf(" %s ", word[j]);
        
        } /* end for */
        printf ( "%s\n", word[0] );
    
        return 0;
    } /* end main */
    
    /* funtion encode */
    void encode( char *dest, char * src)
    {
      char *suffix = "ed";
      strcpy( dest, src+1 );
      strcat( dest, "ed" );
      char *temp = "x";
      temp[0] =*src;
      strcat( dest, temp );
    
    } /* end function encode */
    On the school server:


    ./a.out
    Please enter a line of test. Enter new line to complete.

    Hello how are you
    Segmentation fault
    Last edited by davidjg; 02-10-2011 at 09:11 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
      char *temp = "x";
      temp[0] =*src;
    That's not allowed. String literals are immutable. Try this instead maybe:
    Code:
      strncat( dest, src, 1 );
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    It was worth a try for sure but it's doubling the first character when I add:
    strncat( dest, src, 1)

    Hello how are you
    word count:0 elloedHH
    word count:1 owedhh
    word count:2 reedaa
    word count:3 ouedyy
    ouedyy reedaa owedhh elloedHH

    It's supposed to be stripping the first character and then moving it to the end of the word, then appending "ed"

    Ex:
    elloHed

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What if you just did this then?
    Code:
    void encode( char *dest, char *src )
    {
      sprintf(dest, "%sed%c", src + 1, *src);  // Or is it "%s%ced"? I'm a little confused about where you want the "ed" given your first examples.
    }
    Note: This isn't a recursive function like you originally indicated you had/wanted, but neither was yours.
    Last edited by itsme86; 02-10-2011 at 11:45 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    sprintf(dest, "%s%ced", src + 1, *src);

    OMG!!! That did it!!! THANK YOU!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault: vector to function
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2008, 04:36 AM
  2. A libdc1394 library function is causing a segmentation fault
    By Phanixis in forum Linux Programming
    Replies: 7
    Last Post: 10-16-2007, 12:44 PM
  3. Replies: 2
    Last Post: 10-29-2005, 06:05 PM
  4. segmentation fault - pointers/functions
    By p1c1078 in forum C Programming
    Replies: 15
    Last Post: 04-22-2003, 05:46 PM