简体   繁体   English

检查字符串是否在C中是回文

[英]Check if a string is palindrome in C

i've a question about this code i'm writing for an exercise. 我对我正在练习的这段代码有疑问。 I've to check if a string is palindrome. 我要检查一个字符串是否是回文。 I can't change the declaration of the function.The function only return 1 when all the letters are the same (like "aaaa") but if i charge the sentence with other palindrome (like "anna") the function return me 0 , i can't figure out why this appening.Thank you! 我无法更改该函数的声明。只有当所有字母都相同时该函数才返回1(例如“ aaaa”),但是如果我将该句子归类为其他回文(例如“ anna”),则该函数返回0,我不知道为什么会这样。谢谢!

char* cargar (char*);
int pali (char*);

int main()
{ 
   char*texto=NULL;
   texto=cargar(texto);
   int res=pali(texto);
   if(res==1){printf("\nPalindrome");}
   else printf("\nNot palindrome");

   return 0;
}

char* cargar (char*texto)
{
   char letra;
   int i=0;
   texto=malloc(sizeof(char));
   letra=getche();
   *(texto+i)=letra;
   while(letra!='\r'){
      i++;
      texto=realloc(texto,(i+1)*sizeof(char));
      letra=getche();
      *(texto+i)=letra;}
   *(texto+i)='\0';      
   return texto;
}

int pali (char* texto)
{
   int i;
   for(i=0;*(texto+i)!='\0';i++){
   }i--;
   if(i==0||i==1){return 1;}

   if(*texto==*(texto+i)){
      return pali(++texto);
   }
   else return 0;
}

Your function to determine whether a string is a palindrome is not well thought out. 判断字符串是否为回文符的功能未得到充分考虑。

Let's say you have a string s of length l . 假设您有一个长度为l的字符串s The characters in the string are laid out as: 字符串中的字符布局为:

Indices: 0    1    2    3            l-4  l-3  l-2  l-1
         +----+----+----+----+- ... -+----+----+----+----+
         |    |    |    |    |  ...  |    |    |    |    |   
         +----+----+----+----+- ... -+----+----+----+----+

If the string is a palindrome, 如果字符串是回文,

s[0] = s[l-1]
s[1] = s[l-2]

...

You can stop checking when the index of the LHS is greater or equal to the index of the RHS. 当LHS的索引大于或等于RHS的索引时,您可以停止检查。

To translate that into code, 要将其转换为代码,

int is_palindrome(char const* s)
{
   size_t len = strlen(s);
   if ( len == 0 ) // An empty string a palindrome
   {
      return 1;
   }

   size_t i = 0;
   size_t j = len-1;
   for ( ; i < j; ++i, --j )
   {
      if ( s[i] != s[j] )
      {
         // the string is not a palindrome.
         return 0;
      }
   }

   // If we don't return from inside the for loop,
   // the string is a palindrome.
   return 1;
}

MARCO try this. MARCO试试这个。

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char* cargar (char*);
int pali (char*);

int main()
{

char*texto=NULL;

texto=cargar(texto);

int res=pali(texto);

if(res==strlen(texto)){printf("\nPalindrome");}
else printf("\nNot palindrome");

    return 0;
}


char* cargar (char*texto)
{
char letra;
int i=0;
texto=malloc(sizeof(char));
letra=getche();
*(texto+i)=letra;
while(letra!='\r')
{
    i++;
    texto=realloc(texto,(i+1)*sizeof(char));
    letra=getche();
    *(texto+i)=letra;
}
*(texto+i)='\0';      
return texto;
}

int pali (char* a)
{
int flag=0,i;
int len=strlen(a);
for (i=0;i<len;i++) 
    {
    if(a[i]==a[len-i-1])
         flag=flag+1;
    }
    return flag;
}

You pali function tests if the first character of the string is equal to the last character and then invokes itself for a position of the second character of the string. 您的pali函数测试字符串的第一个字符是否等于最后一个字符,然后调用自身以查找字符串的第二个字符。 Note however, it does not modify the end of a string , so the recursive invocation compares the second character again to the last one. 但是请注意,它不会修改字符串的结尾 ,因此递归调用会将第二个字符再次与最后一个字符进行比较。 Then you compare the third charcter to the last one... Finaly pali returns 1 if all characters are equal to the last one, that is if all are equal. 然后,您将第三个字符与最后一个字符进行比较...如果所有字符都等于最后一个字符 ,也就是说,如果所有字符都相等,则Finaly pali返回1

Try this: 尝试这个:

int pali (char* texto)
{
   char* end;
   for(end = texto; *end != '\0'; end ++)
       ;

   for(--end; texto < end; ++texto, --end) {
      if(* texto != * end)
          return 0;
   }
   return 1;
}

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

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