1

Below is my piece of code, I don't understand why it always gives me the segmentation fault:

#include <stdio.h>
void reverse(void);

int main ()
{
    printf("enter the text");
    printf("\n");
    reverse(); 

    printf("\n");
    return(0);
} 
void reverse(void)
{
    char c;
    if((c=getchar()) != '\n')
    {
        reverse();
    }
    putchar(c);
}

In my opinion I have done everything correctly, what is the mistake?

6
  • 4
    It ran fine for me. Can you tell us which line it seg faults on for you? Commented Jun 16, 2015 at 13:34
  • 1
    "immediate help!" is not the words we like to hear here. Commented Jun 16, 2015 at 13:38
  • maybe you can help us with telling your operation system. Commented Jun 16, 2015 at 13:40
  • @Jaspreet Singh Tell more about the input in which it is failing. Commented Jun 16, 2015 at 14:05
  • 1
    It seems to work as it should, but you're not "reversing a string", you're printing the input in reverse. Commented Jun 16, 2015 at 14:10

3 Answers 3

4

The code works fine as long as you enter a newline. Perhaps you are terminating your input with EOF (usually bound to Ctrl+D) without feeding it a newline before, and in that case, the code will never see a newline and there will be a stack overflow due to infinite recursion.

So, you should check that getchar() doesn't return EOF. Also, getchar() returns int, not char - this is important for portability and to make sure that comparison with EOF works as expected.

Here's the code after addressing these issues:

#include <stdio.h>

void reverse(void);

int main (void) {
    printf("enter the text\n");
    reverse(); 
    printf("\n");
    return 0;
}

void reverse(void) {
    int c;
    if ((c=getchar()) != '\n' && c != EOF) {
        reverse();
    }
    if (c != EOF) {
        putchar(c);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your program compiled and ran fine on my setup: latest stable gcc on Ubuntu 14.04.2 LTS 64-bit.

Here is another version using a different approach (namely the fgets function). See if it works for you:

#include <stdio.h>
#include <string.h>

void reverse_str( char * );

int main()
{
    char input[1024];

        printf("Enter text: ");
        fgets(input, sizeof(input), stdin);

        reverse_str(input);

        printf("Reversed string: %s\n", input);

    return 0;
}

void reverse_str(char *to_reverse)
{
    char temp[1024];
    int count = strlen(to_reverse) - 1; //Exclude newline introduced with fgets
    int i=0;

        for( i=count; i>=0; i-- ){
            temp[i] = to_reverse[count - i - 1]; //Subtract 1 to not include the new line introduced by fgets
        }

        temp[count+1] = '\0';
        strcpy(to_reverse, temp);
}

1 Comment

EDIT: as @molbdnilo pointed out in the comments, with your code you are technically not reversing a string. The function I wrote, on the other hand, does that, so it works for input through stdin as well as other more general applications, as long as the string's length is less than or equal to the temp string length (which you can change).
0

Your code seems to failing because of the nasty characters of getchar()..In most of the system it should work but I think your compiler is trying to access the memory saved beyond the array & hence generating segmentation fault...Can you please make sure if you give '\0' in place of '\n', it is working or not..I think the problem is that your machine is not able to detect the '\n' given from your keyboard & hence keep on going into recursion mode & stack is overflown before the recursion ends & when stack is overflown, it is trying to access unauthorised memory & hence segmentation fault occurs

Try this

#include <stdio.h>
#include <string.h>
char str[] = "Hello World";
size_t length;
int count = 0;

void reverse(char* a, char* b){
//  static int count = 0;
  char temp;
  if (count < length/2){
    count++;
    reverse(str + count, str + (length - 1) - count);
  }
    temp = *a;
    *a = *b;
    *b = temp;
}
int main(){
length = strlen(str);
reverse(str, str + length - 1);
printf("%s", str);
return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.