rtbrown
rtbrown

Reputation: 1

Find palindromes in sentence

I am trying to write a piece of C code that takes a sentence and returns all the palindromes in that sentence, each in a new line. For example, the sentence "I like to race a civic racecar" would return: civic racecar

I've tried to use some debugging software (lldb, as I'm a mac user), but found it a bit confusing. The code below is what I have written. It's returning a segmentation fault, and I'm having trouble identifying it within my program.

int is_palin(char c[], int length) 
{
  int front = 0;
  int back = length - 1;   /*  account for length starting at 0  */  
    if (length % 2 == 0){    /*  check for even palindromes */
          int middle = (length /2) -1 ;
        while (front< middle + 1){
              if (c[front] != c[back]){
                  return 0;}
        front = front + 1;
        back = back -1;

        }  
    }
    else {                                    /*  check for odd palindromes */
          int middle = ((back - 2) / 2 ) + 1; 
              while (front != middle){
                    if (c[front] != c[back]){
                          return 0;}

              front = front + 1;
              back = back -1;}
          }
                return 1;
}

int is_delimiting_char(char ch)
{
if(ch == ' ')     //White space
  return 1;
else if(ch == ',')    //Comma
  return 1;
else if(ch == '.')    //Period
  return 1;
else if(ch == '!')    //Exclamation
  return 1;
else if(ch == '?')    //Question mark
  return 1;
else if(ch == '_')    //Underscore
  return 1;
else if(ch == '-')    //Hyphen
  return 1;
else if(ch == '(')    //Opening parentheses
  return 1;
else if(ch == ')')    //Closing parentheses
  return 1;
else if(ch == '\n')   //Newline (the input ends with it)
  return 1;
else
  return 0;
} 


/////////////////////////////////////////////

//---------------------------------------------------------------------------
// MAIN function
//---------------------------------------------------------------------------


int main (int argc, char** argv) {

  char input_sentence[100];  
  int i=0;
  char current_char;
  int delimiting_char;
  char word[20];
  int word_length;
  int have_palindrome = 0;


  /////////////////////////////////////////////    



  /////////////////////////////////////////////

  /* Infinite loop 
   * Asks for input sentence and prints the palindromes in it
  * Terminated by user (e.g. CTRL+C)
  */

while(1) {

 i=0;       

 print_char('\n'); 

 print_string("input: ");

 /* Read the input sentence. 
  * It is just a sequence of character terminated by a new line (\n)   character.
  */

 do {           
  current_char=read_char();
  input_sentence[i]=current_char;
  i++;
} while (current_char != '\n');

///////////////////////////////////////////// 

print_string("output:\n");
int char_index = 0;         

for(int k=0; k<i; k++)  {   
 palin = 1;
  current_char = input_sentence[k];
  delimiting_char = is_delimiting_char(current_char);


if(delimiting_char) {
  if (char_index > 0) {     //Avoids printing a blank line in case of consecutive delimiting characters.
    word[char_index++] = '\n';    //Puts an newline character so the next word in printed in a new line.
      word_length = word_length + 1;
      if (is_palin(word, word_length) && word_length > 1){
        have_palindrome = 1;
        for(int j=0; j<char_index; j++)  {    
      print_char(word[j]);  
    }
      word_length = 0;
       char_index = 0; 
      }
} }
else {
  word[char_index++] = current_char;
  word_length = word_length + 1;
}

                    }


if (have_palindrome == 0){
  print_string("Sorry!  No palindromes found!"); }
 }

return 0;

}  

Also wondering if anyone has good videos or sites for learnign how to use lldb, when one has never used anything of the sort before. Thanks!

Upvotes: 0

Views: 634

Answers (1)

M Oehm
M Oehm

Reputation: 29136

There are several things wrong here:

  • word_length is uninitialised at first use, so statements like word_length = word_length + 1 lead to undefined behaviour. In fact, you have two different variables, char_index and word_length, that should always have the same value. Instead of going through the hassle to keep them in sync, use just one variable.
  • You reset both char_index and word_length to zero only if a palindrome was found. You should reset if after every word, of course.
  • The line palin = 1; is probably a leftover from older code. You should also reset have_palindrome after each line. In general, you should take more care when defining variables.
  • By adding a newline to your word you make printing a bit easier, but you will never find a palindrome, because the newline at the end is taken into account when checking for the palindrome.
  • Your code for reading with read_char, which is probably an alias to getchar, needs to check for the end of input.
  • You don't need to distinguish between even and odd sized palindromes. Just make the condition that front < back and be done with it. The middle character of an odd sized palindrome doesn't matter. (That's not an error, your code is just needlessly complicated.)

Upvotes: 1

Related Questions