简体   繁体   English

不使用库函数的c中的字符串回文

[英]Palindrome of string in c without using library functions

I actually created the code for palindrome of string using reverse and copy of strings without library function.But still it shows always it is palindrome.What mistake I have done.And it always prints it is always a palindrome regardless whether I give palindrome or not.I checked internet for code but want to know what mistake I made in my code so please help. 我实际上使用反向和字符串复制创建了字符串回文的代码,但没有使用库函数。但是仍然显示总是回文,我犯了什么错误,并且无论我是否给出回文,它总是打印它总是一个回文。我检查了互联网上的代码,但想知道我在代码中犯了什么错误,请提供帮助。

#include <stdio.h>
#include <string.h>
int main()
{
    char s[1000];
    char t[1000],temp;
    int i,j,flag=0;

    gets(s);

    for(i=0;i<strlen(s);i++)
    {
        t[i]=s[i];
    }
    printf("%s",t);
    int n=strlen(s)-1;
    for(j=0,i=n;i>=0;j++,i--)
    {
        t[j]=s[i];
    }
    printf("%s",t);

    for(i=0,j=0;i<=n,j<=n;i++,j++)
    {
        //for(j=0;j<n;j++){
        if(t[j]==t[i])
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
    {
        printf(" palindrome");
    }
    else
    {
        printf("else Palindrome");
    }
    return 0;
}

Irrespective of the rest of the code, what I could not understand is: 无论其余代码如何,我都无法理解:

for(i=0,j=0;i<=n,j<=n;i++,j++)
{
    if(t[j]==t[i])//here you are comparing same indices of the same array,always true
    {
        flag=1;   //executed when i=0,j=0
        break;
    }
}

if(flag==1)
{
    printf(" palindrome");
}
else
{
    printf("else Palindrome");
}

So you are bound to get palindrome all the time. 因此,您一定会一直患有回文症。 You can try this: 您可以尝试以下方法:

int m=n/2,flag=0;
for(int i=0;i<m;i++)
    if(s[i]==s[n-i-1])
        flag++;
if(flag==m)
    //palindrome;
else
    //not

Not preventing using library functions, mistakes in your code are: 在不阻止使用库函数的情况下,代码中的错误是:

  • gets() , which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11, is used. gets() ,它具有不可避免的缓冲区溢出风险,在C99中已弃用并从C11中删除。
  • undefined behavior is invoked at printf("%s",t); 未定义的行为printf("%s",t);处调用printf("%s",t); , where pointer to what is not a null-terminated string is passed for use with %s . ,其中将指向非空终止字符串的指针传递给%s
  • The condition t[j]==t[i] is wrong because the same things are always compared. 条件t[j]==t[i]是错误的,因为总是比较相同的事物。
  • You used flag to check if " at least any one of the characters is same". 您使用了flag来检查“ 至少一个字符是否相同”。 You should check " all of the characters are same". 您应该选中“ 所有字符都相同”。

Try this: 尝试这个:

#include<stdio.h>
#include<string.h>
int main(void)
{
    char s[1000],*lf;
    char t[1000],temp;
    int i,j,n,flag=1;

    /* use fgets() instead of gets() */
    fgets(s, sizeof(s), stdin);
    if((lf=strchr(s, '\n'))!=NULL)
    {
        *lf = '\0';
    }

    n=strlen(s);
    for(i=0;i<n;i++)
    {
        t[i]=s[i];
    }
    /* terminate the string */
    t[n]='\0';
    printf("%s",t);

    n=strlen(s)-1;
    for(j=0,i=n;i>=0;j++,i--)
    {
        t[j]=s[i];
    }
    printf("%s",t);

    for(i=0;i<=n;i++)
    {
        /* check if all characters are same */
        if(s[i]!=t[i])
        {
            flag=0;
            break;
        }
    }

    if(flag==1)
    {
        printf(" palindrome");
    }
    else
    {
        printf("else Palindrome");
    }
    return 0;
}

Library functions are not avoided in this code. 此代码中未避免使用库函数。 You can avoid using them by implementing what is (somewhat) equivalent to them by yourself and replacing usage of library functions to your implimentation. 您可以通过自己实现(某种程度上)等同于它们(并在某种程度上代替库函数的用法)来避免使用它们。

I think the requirement of the assignment means that you may not use standard string functions. 我认为分配的要求意味着您不能使用标准的字符串函数。

To determine whether a string is a palindrome there is no need to create a copy of the string and to reverse the copy. 要确定一个字符串是否是回文,就无需创建该字符串的副本并反转该副本。 You can check this "in place". 您可以选中此“到位”。

Here is a demonstrative program 这是一个示范节目

#include <stdio.h>

int is_palindrome( const char *s )
{
    size_t n = 0;

    while ( s[n] != '\0' ) n++;

    size_t i = 0;

    while ( i < n / 2 && s[i] == s[n-i-1] ) i++;

    return i == n / 2;
}

#define  N  100

int main( void ) 
{
    char s[N];


    fgets( s, sizeof( s ), stdin );

    size_t n = 0;

    while ( s[n] != '\0' && s[n] != '\n' ) n++;

    if ( s[n] ) s[n] = '\0';

    if ( is_palindrome( s ) ) printf( "%s is a palindrome\n", s );

    return 0;
}

For example if to enter string ABCDEDCBA then the program output will be 例如,如果输入字符串ABCDEDCBA则程序输出为

ABCDEDCBA is a palindrome

As for your program then it uses the standard string function strlen . 对于您的程序,它使用标准字符串函数strlen The copy of the original string is not zero terminated. 原始字符串的副本不是零终止的。 And this loop 而这个循环

for(i=0,j=0;i<=n,j<=n;i++,j++)
{
//for(j=0;j<n;j++){
if(t[j]==t[i])
{

flag=1;
break;

 }

does not make sense even if you used correct indices. 即使您使用正确的索引也没有意义。

Take into account that function gets is unsafe and is not supported by the C Standard any more. 考虑到功能gets是不安全的,并且C标准不再支持该功能。 Instead use standard function fgets that appends inputted strings with the new line character (if there is enough space in the string) that you should to remove. 而是使用标准函数fgets ,该函数将输入的字符串附加换行符(如果字符串中有足够的空格),应将其删除。

Also function main without parameters in C should be declared like 同样在C中不带参数的main函数应声明为

int main( void )

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

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