Advertisement
Guest User

Untitled

a guest
May 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int is_palindrome(const char* str, size_t length){
  4.   int i = 0;
  5.   int j = length - 1;
  6.  
  7.   for(;j>=i && *(str +i) == *(str+j);i++,j--);
  8.  
  9.   return j <= i;
  10. }
  11.  
  12. void palindrome_by_line(const char* str){
  13.   unsigned current_length;
  14.   int current_line = 1;
  15.   int amount;
  16.   char* inner_str;
  17.   char* inner_inner_str;
  18.   //controls the whole string
  19.   while(*str){
  20.     while(*(str) && *(str) == ' ') str++;
  21.     inner_str = (char*)str;
  22.     //controls until line feed
  23.     amount =0;
  24.     while((*inner_str) && *(inner_str) != '\n'){
  25.       inner_inner_str = inner_str;
  26.      
  27.      
  28.       //controls until space is found
  29.       current_length = 0;
  30.       while((*inner_inner_str) && (*inner_inner_str) != '\n' && *(inner_inner_str) != ' '){
  31.         current_length++;
  32.         inner_inner_str++;
  33.        
  34.       }
  35.      
  36.       if(is_palindrome(inner_inner_str - current_length , (inner_inner_str - inner_str)))
  37.         amount++;
  38.      
  39.       while(*(inner_inner_str) && *(inner_inner_str) == ' ') inner_inner_str++;
  40.      
  41.      
  42.       inner_str += (inner_inner_str - inner_str);
  43.     }
  44.    
  45.     printf("Cantidad de palindromos en esta linea %d: %d\n", current_line, amount);
  46.    
  47.     current_line++;
  48.     if(*inner_str == '\n')
  49.       str += (inner_str - str) + 1;
  50.     else
  51.       str += (inner_str - str);
  52.   }
  53. }
  54.  
  55. int main(void) {
  56.   palindrome_by_line("    menem 121\n radar\nneuquen reconocer\nanitalavalatina\n");
  57.   return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement