-1

I'm asked to check whether a given string can be a palindrome after rearranging and then return true if it can be a palindrome or false if it cannot be a palindrome.

I'm getting a runtime error: Segmentation fault while running tests.

Here's my code:

bool palindromeRearranging(char * inputString) {
    
    int index;
    int count, count1 = 0;
    
    for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
    {    count = 0;
        if(inputString[i] == '*')
        {
            continue;
        }else 
        {        
            for(int j = i+1; j < b ;j++)
            {           
              if(inputString[i] == inputString[j] )
                {                     
                  inputString[j] = '*';
                  count++;
                  index = i;
                }
          
            }
         inputString[index] = '*';
        }
        
        if(count %2 == 0 && count != 0)
        {
            count1++;
        }
    }
    
    for(int i = 0, b = strlen(inputString); i < b; i++)
    {
        if(inputString[i] != '*')
        {
            count1++;
        }
    }
 
    if(count1 > 1)
    {
        return false;
    }else
    {
        return true;
    }

}

Here inputString is the string given. I tried to replace the two characters with * if they are both same. And then counting the no. of single characters.

5 out of 10 test cases have passed.

For example test cases like "abbcabb", "aabb", "zyyzzzzz" have passed.

But I'm getting runtime error: Segmentation fault for test cases like "abcad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa". "abca", "abdhuierf".

I hope you could help me with this one.

Btw I'm solving this on codesignal.com, under the arcade section/intro. Q.18 palindromeRearranging.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
josh_3501
  • 15
  • 2

3 Answers3

2

As it was already mentioned you are using the non-initialized variable index.

int index;

in this for loop

for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
{    count = 0;
    if(inputString[i] == '*')
    {
        continue;
    }else 
    {        
        for(int j = i+1; j < b ;j++)
        {           
          if(inputString[i] == inputString[j] )
            {                     
              inputString[j] = '*';
              count++;
              index = i;
            }
      
        }
     inputString[index] = '*';
    }

if in the inner for loop of the else statement a repeated character was not found that results in undefined behavior.

Or you can use an invalid value of the variable index that it keeps after a previous iteration of the loop.

Also not all characters that satisfy the condition

          if(inputString[i] == inputString[j] )

are substituted for the character '*'.

But in any case your approach is invalid. You shall not change the original string. Otherwise after calling your function the caller will deal with a modified string.

The function can be written for example the following way as it is shown in the demonstrative program below.

#include <stdio.h>

int can_be_palindrome( const char *s )
{
    size_t odd = 0;
    
    for ( const char *p = s; odd < 2 && *p; ++p )
    {
        const char *q = s;
        
        while ( q != p && *q != *p ) ++q;
        
        if ( q == p )
        {
            size_t n = 1;
            while ( *++q )
            {
                if ( *q == *p ) ++n;
            }
            
            odd += n % 2;
        }
    }
    
    return odd < 2;
}

int main(void) 
{
    const char *s = "abbcabb";
    
    printf( "\"%s\" can be a palindrome is %s\n", 
            s, can_be_palindrome( s ) ? "true" : "false" );
            
    s = "aabb";
    
    printf( "\"%s\" can be a palindrome is %s\n", 
            s, can_be_palindrome( s ) ? "true" : "false" );
            
    s = "zyyzzzzz";
    
    printf( "\"%s\" can be a palindrome is %s\n", 
            s, can_be_palindrome( s ) ? "true" : "false" );
            
    s = "abca";
    
    printf( "\"%s\" can be a palindrome is %s\n", 
            s, can_be_palindrome( s ) ? "true" : "false" );
            
    return 0;
}

The program output is

"abbcabb" can be a palindrome is true
"aabb" can be a palindrome is true
"zyyzzzzz" can be a palindrome is true
"abca" can be a palindrome is false
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

"Why am I getting “runtime error: Segmentation fault” for random test cases?"

Aside from your function not being logically viable for testing palindromes, it will fail on random occasions due to undefined behavior:

int index;

Is not initialized,

It fails here for example when its value exceeds the array index:

inputString[index] = '*';

With the following sequence of run-time messages:

![enter image description here

To address this, change this:

int index;
int count, count1 = 0;

to this:

int index = 0;
int count = 0, count1 = 0;

Aside, here is an example of a similar question and answer.

ryyker
  • 22,849
  • 3
  • 43
  • 87
-1

Error is due to unintialized behaviour of your int index;

Aditya Aggarwal
  • 159
  • 1
  • 9
  • FYI - While in the edit box, the red squiggly lines beneath _unintialized_ and _bhehaviour_, indicate mis-spelled words. If you right click your mouse on these words, a list of suggested spelling corrections will appear. – ryyker Jun 09 '21 at 12:34
  • You also know that you can edit your answer any time? Just click _edit_ below the answer. (and I am not the down voter.) – ryyker Jun 09 '21 at 13:48
  • Buddy i came here to gain and give the knowledge i have not for votes :) – Aditya Aggarwal Jun 09 '21 at 13:49