简体   繁体   中英

Palindrome/mini-Palindrome in string

I need to check if a given string is a Palindrome or mini-Palindrome. Palindrome length will be 2 or more, the function need to ignore spaces and ignore the differences of upper and lower alphabet. if the string is Palindrome the function will transfer the indexes of the start and the end of him and will return 1 else return 0. example1: "My gym" the function will transfer low=0 high=5 and 1 example2: "I Love ANNA" the function will transfer low=7 high=10 and 1 example3: "I love Pasta" return 0.

Also i can't use functions from librarys other then string.h stdlib.h stdio.h.

I tried to write like this:

    int i;
int size = strlen(str);
i = 0;
while (str[i] != '\0')
{
    if (str[i] == ' ')
    {
        i++;
        continue;
    }
    //-------------------
    if (str[i] >= ‘a’ && str[i] <= ‘z’)
        str[i] = str[i] - 32;
    if (str[size-1] >= ‘a’ && str[size-1] <= ‘z’)
        str[size-1] = str[size-1] - 32;
    //-------------------
    if (str[i] == str[size-1])
    {
        *low = i;
        *high = size-1;
        return 1;
    }
    else
    {
        size--;
        i++;
    }
}
return 0;

But it isnt working well, i cant figure how to do it with the example 2

Here goes. Will this help you

#define LOWER(a) (((a) >=' A' && (a) <= 'Z') ? ((a) - 'A' +'a') : (a))
#define MYCMP(a,b) (LOWER(a) == LOWER(b))

    int is_palidrome(char *s) {
      int start = 0;
      int end = strlen(s) - 1;
       
      for (; s[start] // Not end of line
             && end >=0 // Not run over the from of the line
             && start < end // Still not got to the middle
             && MYCMP(s[start], s[end]) == 1; // They are still equal
             start++, end--) { //Nowt  }
      };
    
      return (start >= end);
    }

I made a program. It works only if the string contains letters and spaces. You can modify it to work for other characters.

#include <stdio.h>
#include <string.h>

#define SIZE 100

int isPalindrome( char *s, size_t l );

int main() {
    
    char str[SIZE];
    size_t i, j, len, pldrm = 0;
    
    fgets(str, SIZE, stdin);
    
    len = strlen(str);
    
    for(i = 0; i < len; i++) if( str[i] != ' ' && !((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) ) goto the_end;
    
    for(i = 0; i < len-1; i++) {
        
        if( str[i] != ' ' ) {
        
            for(j = i+1; j < len; j++) {
            
                if( (pldrm = isPalindrome(&str[i], j-i+1)) ) {
                
                    str[j+1] = '\0';
                    goto the_end;
                }
            }
        }
    }
    
the_end:
    
    pldrm ? printf("A palindrome has been found from the position %zu till the position %zu.\n\nThe palindrome is: %s\n", i, j, &str[i]) : puts("No palindromes");
    
    return 0;
}

int isPalindrome( char *s, size_t l )
{
    static const char az[26] = "abcdefghijklmnopqrstuvwxyz", AZ[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int isPldrm = 1, spc = 0; // used to skip spaces within the palindrome

    for(size_t i = 0; i < l/2; i++) {
        
        for(size_t j = 0; j < 26; j++) {
            
            if( s[i] == az[j] || s[i] == AZ[j] ) {

                while( s[l-1-i-spc] == ' ' ) ++spc;
                
                if( s[l-1-i-spc] != az[j] && s[l-1-i-spc] != AZ[j] ) {
                    isPldrm = 0;
                    goto thats_it;
                }
                break;
            }
        }
    }
    
thats_it:
    
    return isPldrm;
}

Also, it finds only the first palindrome in the input. Doesn't check for further palindromes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM