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?

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.

#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 */

This program was written in Netbeans 6.9.1 using the Cygwin compiler. When I run the same program on the school compiler which is probably older, I get a seg fault. Any ideas on what to do here?


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

Hello how are you
Segmentation fault

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.