Yonka2019

Untitled

Jan 30th, 2021 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define MAX_STR_LEN 102
  7.  
  8. void removeAllChars(char str[], char toRemove[]);
  9. bool isExist(char str[], char search[]);
  10. bool isPalindrome(char str[]);
  11. void lowerString(char str[]);
  12.  
  13.  
  14. int main()
  15. {
  16.     char str[MAX_STR_LEN] = { 0 };
  17.     printf("Enter string (max length 100 chars): ");
  18.  
  19.     fgets(str, MAX_STR_LEN, stdin); //get input
  20.     str[strcspn(str, "\n")] = 0; //removes the \n
  21.  
  22.     //string modifying
  23.     lowerString(str); //lower the string
  24.     removeAllChars(str, " "); //remove all spaces (escape whitespaces)
  25.  
  26.     if (isPalindrome(str))
  27.     {
  28.         printf("Yes\n");
  29.     }
  30.     else
  31.     {
  32.         printf("No\n");
  33.     }
  34.  
  35.     return 0;
  36. }
  37. /*
  38. Removes all overlaps of string2 in string1 ! CASE SENSITIVE !
  39. input: string1, string2
  40. output: -
  41. example:
  42.     before: this is test string (string to remove = "i")
  43.     after: ths s test strng
  44.     (all "i" got removed from the string)
  45. */
  46. void removeAllChars(char str[], char toRemove[])
  47. {
  48.     while (isExist(str, toRemove))
  49.     {
  50.         int index = strcspn(str, toRemove), i = 0;
  51.  
  52.         for (i = index; str[i]; i++)
  53.         {
  54.             str[i] = str[i + 1];
  55.         }
  56.     }
  57. }
  58. /*
  59. Checks if string2 characters exist in string1
  60. input: string1, string2
  61. output: true - if exist, false - if not
  62. */
  63. bool isExist(char str[], char search[])
  64. {
  65.     if (strcspn(str, search) == strlen(str)) //if char doesn't exist in the string, it's returns the length of the string = FALSE (char doesn't exist.)
  66.     {
  67.         return false;
  68.     }
  69.     else
  70.     {
  71.         return true;
  72.     }
  73. }
  74. /*
  75. Checks if the given string palindrome
  76. input: string
  77. output: true if palindrome, false if not
  78. */
  79. bool isPalindrome(char str[])
  80. {
  81.     int i = 0, j = 0;
  82.  
  83.     for (i = 0, j = strlen(str) - 1; str[i]; i++, j--)
  84.     {
  85.         if (str[i] != str[j])
  86.         {
  87.             return false;
  88.         }
  89.     }
  90.     return true;
  91. }
  92. /*
  93. lowers all chars in the string
  94. input: string
  95. output: -
  96. example:
  97.     before: This Is Test String
  98.     after: this is test string
  99. */
  100. void lowerString(char str[])
  101. {
  102.     int i = 0;
  103.  
  104.     for (i = 0; str[i]; i++){
  105.         str[i] = tolower(str[i]);
  106.     }
  107. }
  108.  
Add Comment
Please, Sign In to add comment