Idanref

5.c

Jan 11th, 2021 (edited)
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void purify(char *s) // A function to turn strings to all-lowercase letters
  6. {
  7.     for(int i = 0; s[i] != '\0'; i++)
  8.     {  
  9.         if(s[i] >= 'A' && s[i] <= 'Z')
  10.         {
  11.             s[i] += 32;
  12.         }      
  13.     }
  14. }
  15.  
  16. int remove_spaces(char *str)
  17. {
  18.     int len = strlen(str);
  19.     int newlen=0;
  20.     int spaces = 0;
  21.    
  22.     for (int i = 0; i < len; i++)
  23.     {
  24.         if(str[i]!= ' ')
  25.         {
  26.             newlen++;
  27.         }
  28.  
  29.         else if(str[i] == ' ')
  30.             spaces++;
  31.        
  32.     }
  33.  
  34.     int index =0;
  35.    
  36.     for (int j = 0; j < len; j++)
  37.     {
  38.         if(str[j]!=' ')
  39.         {
  40.             str[index]=str[j];
  41.             index++;
  42.         }
  43.     }
  44.     str[newlen]='\0';
  45.  
  46.     return spaces;
  47. }
  48.  
  49. int check_small_palindrome(char *s, int first, int last)
  50. {
  51.     char *new_string = (char*)malloc(last-first+1+1); // +1 for '\0'
  52.  
  53.     int new_string_index = 0;
  54.  
  55.     for(int k = first; k <= last; k++)
  56.     {
  57.         new_string[new_string_index] = s[k];
  58.         new_string_index++;
  59.     }
  60.  
  61.     // new_string[last-first+1] = '\0';
  62.  
  63.     int spaces = remove_spaces(new_string);
  64.  
  65.     int len_new_string = last-first + 1;
  66.     len_new_string -= spaces;
  67.  
  68.     for(int i = 0; i < len_new_string; i++)
  69.     {
  70.         if(new_string[i] != new_string[len_new_string-1])
  71.         {
  72.             free(new_string);
  73.             return 0;
  74.         }
  75.  
  76.         else
  77.             len_new_string--;
  78.  
  79.     }
  80.  
  81.     free(new_string);
  82.  
  83.     return 1;
  84. }
  85.  
  86.  
  87. int isPalindrome(char *s, int *pass1, int *pass2)
  88. {
  89.     purify(s);
  90.     int len = strlen(s);
  91.  
  92.     int first = 0, last = len - 1;
  93.  
  94.  
  95.     for(int i = 0; i < len; i++)
  96.     {
  97.         if(s[i] != ' ')
  98.         {
  99.             first = i;
  100.  
  101.             for(int j = last; j > i; j--)
  102.             {
  103.                 if(s[i] == s[j] && s[i] != ' ' && s[j] != ' ')
  104.                 {
  105.                     last = j;
  106.  
  107.                     if(check_small_palindrome(s, first, last))
  108.                     {
  109.                         *pass1 = first;
  110.                         *pass2 = last;
  111.                         return 1;
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     return 0;
  119.  
  120. }
  121.  
  122. void main()
  123. {
  124.  
  125.     // time complexity 'n'
  126.     // get a char every time and compare the char with the new array
  127.     // that holds everything except the char
  128.     // the char can be replaced with ' '
  129.  
  130.     // char s[] = "I love anna aoi";
  131.     // char s[] = "l";
  132.     // char s[] = "I ' 'ove anna aoi";
  133.  
  134.     char s[] = "I anna plnas";
  135.     int pass1, pass2;
  136.  
  137.     int result = isPalindrome(s, &pass1, &pass2);
  138.  
  139.     // printf("%s\n\n", s);
  140.     printf("\nMoney Time: %d\n", result);
  141.     printf("\n1: %d\n2: %d\n\n", pass1, pass2);
  142. }
Add Comment
Please, Sign In to add comment