Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:01:39+00:00 2026-05-26T05:01:39+00:00

Just for fun I have written a function to check if string given is

  • 0

Just for fun I have written a function to check if string given is palindrome. When I run the prog it throws segmentation fault. Could anyone please throw light on it.

int palindrome( const char *input )         
{    
  char * reverse;    
  int len     = 0 ;     
  int i       = 0;     
  bool result = false;    
  len         = strlen(input);    

  if( len <= 1)   
    return  -1;     
  reverse  = (char *)malloc( sizeof ( char)* len);     
  printf( " the len of character is %d", len);        
  while( input[i++] != '/0')     
  {     
     reverse[ --len] = input[i];         
  }
  reverse[len] = '/0';

 printf(" the reverse string is %s", reverse);
 if( !strcmp( input, reverse) )
   return 1;
 else
 return 0;
}

Thanks
Sam


Changing the code as per suggestions to:

int palindrome( const char *input )         
{    
  char * reverse;    
  int len     = 0 ;     
  int i       = 0;     
  bool result = false;    
  len         = strlen(input)+1;    

  if( len <= 2)   
    return  -1;     
  reverse  = (char *)malloc(len);     
  printf( " the len of character is %d", len); 
  reverse[len] = '\0' ;      
  while( input[i++] != '\0')     
  {     
     reverse[ --len] = input[i];         
  }

 printf(" the reverse string is %s", reverse);
 if( !strcmp( input, reverse) )
   return 1;
 else
 return 0;
}

I still have a problem. The segmentation fault has disappeared but the reversed string is empty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:01:39+00:00Added an answer on May 26, 2026 at 5:01 am

    A few points:

    • the null terminator for strings is '\0', not '/0'.
    • sizeof(char) is always 1, you don’t need to multiply by it.
    • you should always allocate enough space for a string and its null terminator such as with strlen(s) + 1.
    • you should not cast the return value from malloc in C, it hides certain errors that you would be better off knowing about.
    • since you’re gradually reducing len to populate the reverse string, it will end up as 0, not usable for placing the null terminator at the end of that string. This is probably the immediate cause of your core dump since calling strcmp on a non-null-terminated string is a bad idea.
    • it’s not really necessary to create a reversed string at all, you can just check the first and last characters, then the second and second-to-last and so on (until they meet in the middle, or cross over).

    On that last point, what I mean is something like (pseudo-code):

    def isPalindrome (str):
        left = 0
        right = strlen(str) - 1
        while left < right:
            if str[left] <> str[right]:
                return false
            left = left + 1
            right = right - 1
        return true
    

    And, with the update, you have the right idea, setting the null terminator fist before reducing len.

    But since, len is now the string length plus one, you’re populating indexes 1 thru LEN rather than 0 thru LEN-1.

    Change:

    reverse[len] = '\0';
    

    to:

    reverse[--len] = '\0';
    

    Or, better yet, change it so that len is still the length of the string (for printf):

    len = strlen (input) + 1;
    :
    reverse = malloc (len);
    

    to:

    len = strlen (input);
    :
    reverse = malloc (len + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a basic planet viewer OpenGL Perl application, just for fun. I have
I have written a small function, which uses ElementTree and xpath to extract the
I have used opera unite for deploying a static html/css/javascript website just for fun.
Asking a simple question, just want everyone have fun to solve it. I got
just for fun... Is it possible to have a .js file that loads jquery
I'm programming a little Twitter Client just for fun. I have the tweet's text
I am playing around some in C++, just for fun. I have just begun
So I now have a fairly complete LISP (scheme) interpreter written in haskell. Just
I have done a few sample projects (just for fun) using Silverlight deep zoom.
I have a zap() function written to deallocate a 1-d array as follows. void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.