Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <ctype.h>
- #define MAX_STR_LEN 102
- void removeAllChars(char str[], char toRemove[]);
- bool isExist(char str[], char search[]);
- bool isPalindrome(char str[]);
- void lowerString(char str[]);
- int main()
- {
- char str[MAX_STR_LEN] = { 0 };
- printf("Enter string (max length 100 chars): ");
- fgets(str, MAX_STR_LEN, stdin); //get input
- str[strcspn(str, "\n")] = 0; //removes the \n
- //string modifying
- lowerString(str); //lower the string
- removeAllChars(str, " "); //remove all spaces (escape whitespaces)
- if (isPalindrome(str))
- {
- printf("Yes\n");
- }
- else
- {
- printf("No\n");
- }
- return 0;
- }
- /*
- Removes all overlaps of string2 in string1 ! CASE SENSITIVE !
- input: string1, string2
- output: -
- example:
- before: this is test string (string to remove = "i")
- after: ths s test strng
- (all "i" got removed from the string)
- */
- void removeAllChars(char str[], char toRemove[])
- {
- while (isExist(str, toRemove))
- {
- int index = strcspn(str, toRemove), i = 0;
- for (i = index; str[i]; i++)
- {
- str[i] = str[i + 1];
- }
- }
- }
- /*
- Checks if string2 characters exist in string1
- input: string1, string2
- output: true - if exist, false - if not
- */
- bool isExist(char str[], char search[])
- {
- 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.)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- /*
- Checks if the given string palindrome
- input: string
- output: true if palindrome, false if not
- */
- bool isPalindrome(char str[])
- {
- int i = 0, j = 0;
- for (i = 0, j = strlen(str) - 1; str[i]; i++, j--)
- {
- if (str[i] != str[j])
- {
- return false;
- }
- }
- return true;
- }
- /*
- lowers all chars in the string
- input: string
- output: -
- example:
- before: This Is Test String
- after: this is test string
- */
- void lowerString(char str[])
- {
- int i = 0;
- for (i = 0; str[i]; i++){
- str[i] = tolower(str[i]);
- }
- }
Add Comment
Please, Sign In to add comment