#include<stdio.h>
#include <stdlib.h>

#define MAXLINE 10000
void reverse(char *str);
int main()
{
        int c, i;
        char j;
        char S[MAXLINE], *p1;
        p1=S;

        for(c=getchar(); c!=EOF; i++)
        {
        S[i]=c;
        }
        reverse(p1);
return 0;
}

void reverse(char *str)
{
        if(*str)
                {
                        reverse(str+1);
                        putchar(*str);
                }
}

Please help me correct the segmentation fault.

blade71(107)% gcc -o reverse reverse_2.c
blade71(108)% reverse<reverse.in>reverse.out
Segmentation fault (core dumped)
blade71(109)%

Cat of files and octal dumps in hex:

Have you heard of palindromes?

In the garden of Eden, on meeting:

"Madam, I'm Adam."

Napoleon on his capability after his first defeat:

"Able was I ere I saw Elba."

And of course, on diet:

"Doc, note: I dissent! A fast never prevents a fatness. I diet on cod."

blade71(114)% od -x reverse.in
0000000 4861 7665 2079 6f75 2068 6561 7264 206f
0000020 6620 7061 6c69 6e64 726f 6d65 733f 0a0a
0000040 496e 2074 6865 2067 6172 6465 6e20 6f66
0000060 2045 6465 6e2c 206f 6e20 6d65 6574 696e
0000100 673a 0a0a 224d 6164 616d 2c20 4927 6d20
0000120 4164 616d 2e22 0a0a 4e61 706f 6c65 6f6e
0000140 206f 6e20 6869 7320 6361 7061 6269 6c69
0000160 7479 2061 6674 6572 2068 6973 2066 6972
0000200 7374 2064 6566 6561 743a 0a0a 2241 626c
0000220 6520 7761 7320 4920 6572 6520 4920 7361
0000240 7720 456c 6261 2e22 0a0a 416e 6420 6f66
0000260 2063 6f75 7273 652c 206f 6e20 6469 6574
0000300 3a0a 0a22 446f 632c 206e 6f74 653a 2049
0000320 2064 6973 7365 6e74 2120 4120 6661 7374
0000340 206e 6576 6572 2070 7265 7665 6e74 7320
0000360 6120 6661 746e 6573 732e 2049 2064 6965
0000400 7420 6f6e 2063 6f64 2e22 0a0a
0000414

Your code has three immediate problems:

  1. The i variable is not initialized to 0.
  2. getchar() is only called once, not every time the loop iterates.
  3. The array is not terminated with a '\0' character.

If you fix those things, it should work.

Your code has three immediate problems:

  1. The i variable is not initialized to 0.
  2. getchar() is only called once, not every time the loop iterates.
  3. The array is not terminated with a '\0' character.

If you fix those things, it should work.

I have initialized the variable and fixed the problem with getchar. I don't quite understand your third advice however. Could you elaborate more please?

In the reverse() function, there is a test for if(*str) . That means the function is expecting str to be a string. Strings in C are sequences of characters where the last character is a sentinel with the value of 0. This value is not added automatically, you have to insert it because you are manually building a string:

S[i] = '\0';
reverse(p1);

In the reverse() function, there is a test for if(*str) . That means the function is expecting str to be a string. Strings in C are sequences of characters where the last character is a sentinel with the value of 0. This value is not added automatically, you have to insert it because you are manually building a string:

S[i] = '\0';
reverse(p1);

so shouldn't the assignment read S[i-1]='\0'? Otherwise by putting the assignment you gave me in the body of my for loop every element of array S will be initialized to NULL?

so shouldn't the assignment read S[i-1]='\0'?

At the end of the loop, i is indexing the right array element for placing '\0'. If you subtract 1, you will overwrite the last character. This problem is easier to see if you change the loop to stop on a new line:

for (i = 0; (c = getchar()) != '\n'; ++i)

Otherwise by putting the assignment you gave me in the body of my for loop every element of array S will be initialized to NULL?

That is why I did not put the assignment in the body of the loop. I put it after the loop, just before the call to reverse().

blade71(43)% gcc -o reverse reverse_2.c
blade71(44)% reverse<reverse.in>reverse.out
blade71(45)% cat reverse.out


".doc no teid I .ssentaf a stneverp reven tsaf A !tnessid I :eton ,coD"

:teid no ,esruoc fo dnA

".ablE was I ere I saw elbA"

:taefed tsrif sih retfa ytilibapac sih no noelopaN

".madA m'I ,madaM"

:gniteem no ,nedE fo nedrag eht nI

?semordnilap fo draeh uoy evaHblade71(46)% od -x reverse_2.c
0000000 2369 6e63 6c75 6465 3c73 7464 696f 2e68
0000020 3e0a 2369 6e63 6c75 6465 203c 7374 646c
0000040 6962 2e68 3e0a 0a23 6465 6669 6e65 204d
0000060 4158 4c49 4e45 2031 3030 3030 0a76 6f69
0000100 6420 7265 7665 7273 6528 6368 6172 202a
0000120 7374 7229 3b0a 696e 7420 6d61 696e 2829
0000140 0a7b 0a09 696e 7420 632c 2069 3b0a 0963
0000160 6861 7220 6a3b 200a 0963 6861 7220 535b
0000200 4d41 584c 494e 455d 2c20 2a70 313b 0a09
0000220 7031 3d53 3b09 0a09 6320 3d20 6765 7463
0000240 6861 7228 293b 0a09 666f 7228 693d 303b
0000260 2063 213d 454f 463b 2063 3d67 6574 6368
0000300 6172 2829 290a 097b 0a09 535b 695d 3d20
0000320 633b 0a09 692b 2b3b 0a09 7d0a 0953 5b69
0000340 5d20 3d20 275c 3027 3b0a 0972 6576 6572
0000360 7365 2870 3129 3b0a 7265 7475 726e 2030
0000400 3b0a 7d0a 0a76 6f69 6420 7265 7665 7273
0000420 6528 6368 6172 202a 7374 7229 0a7b 0a09
0000440 6966 282a 7374 7229 0a09 097b 0a09 0909
0000460 7265 7665 7273 6528 7374 722b 3129 3b0a
0000500 0909 0970 7574 6368 6172 282a 7374 7229
0000520 3b0a 0909 7d0a 7d0a 0a00
0000531

YOU ARE THE MAN!

Code updated:

#include<stdio.h>
#include <stdlib.h>

#define MAXLINE 10000
void reverse(char *str);
int main()
{
        int c, i;
        char j;
        char S[MAXLINE], *p1;
        p1=S;
        c = getchar();
        for(i=0; c!=EOF; c=getchar())
        {
        S[i]= c;
        i++;
        }
        S[i] = '\0';
        reverse(p1);
return 0;
}

void reverse(char *str)
{
        if(*str)
                {
                        reverse(str+1);
                        putchar(*str);
                }
}
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.