簡體   English   中英

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

[英]Palindrome/mini-Palindrome in string

我需要檢查給定的字符串是回文還是迷你回文。 回文長度將是 2 或更多,function 需要忽略空格並忽略上下字母的差異。 如果字符串是回文,則 function 將傳輸他的開始和結束的索引,並將返回 1 否則返回 0。示例 1:“我的健身房” function 將傳輸低 = 0 高 = 5 和 1 示例2:“我愛ANNA" function 將傳輸 low=7 high=10 和 1 example3: "I love Pasta" 返回 0。

我也不能使用 string.h stdlib.h stdio.h 以外的庫中的函數。

我試着這樣寫:

    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;

但它工作得不好,我不知道如何用示例 2 來做

開始。 這對你有幫助嗎

#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);
    }

我做了一個程序。 僅當字符串包含字母和空格時才有效。 您可以修改它以適用於其他角色。

#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;
}

此外,它只找到輸入中的第一個回文。 不檢查進一步的回文。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM