简体   繁体   English

字符串中的回文/迷你回文

[英]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.回文长度将是 2 或更多,function 需要忽略空格并忽略上下字母的差异。 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.如果字符串是回文,则 function 将传输他的开始和结束的索引,并将返回 1 否则返回 0。示例 1:“我的健身房” function 将传输低 = 0 高 = 5 和 1 示例2:“我爱ANNA" function 将传输 low=7 high=10 和 1 example3: "I love Pasta" 返回 0。

Also i can't use functions from librarys other then string.h stdlib.h stdio.h.我也不能使用 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但它工作得不好,我不知道如何用示例 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.不检查进一步的回文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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