简体   繁体   English

使用C中的函数检查字符串是否是回文

[英]Checking whether string is palindrome or not using Functions in C

I want to check whether a string entered by the user is palindrome or not. 我想检查用户输入的字符串是否是回文。 Without using functions I have done this problem but when I use functions for this problem I am always getting the same output: 没有使用函数我就解决了这个问题,但是当我使用函数解决这个问题时,我总是得到相同的输出:

The Entered String Is Not Palindrome! 输入的字符串不是回文!

Even if the string entered by the user is palindrome I get the same output. 即使用户输入的字符串是回文,我也会得到相同的输出。 Here is my code: 这是我的代码:

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

void reverse(char [],int);

int main() {
    char a[100];int len, i = 0;

    printf("Please enter the string to check: \n");
    fgets(a,100,stdin);

    //Loop to replace the '\n' inserted at the end of string due to fgets() with '\0'
    for (int i = 0; i < strlen(a); ++i)
    {
        if (a[i] == '\n')
        {
            a[i] = '\0';
        }
    }

    len = strlen(a);

    reverse(a,len);

    return 0;
}

void reverse(char b[100], int n) {
    char c[100];int k=0;

    //Read characters from b[] from the end and store them into c[]
    for (int i = n-1; i >= 0; i--)
    {
        c[k] +=b[i];
        k++;
    }
    c[k] = '\0';

    //Check if the reversed string c[] and the actual string b[] are equal
    if(strcmp(c,b) == 0)
        printf("The Entered String Is Palindrome!\n");
    else
        printf("The Entered String Is Not Palindrome!\n");
}

The code in the reverse() function is the same as the one I used to solve the same problem without functions(and that program worked perfectly fine). reverse()函数中的代码与我用来解决相同问题而不使用函数的代码相同(该程序运行得很好)。 But still it isn't giving the correct output. 但是它仍然没有给出正确的输出。 What am I doing wrong here? 我在这里做错了什么?

EDIT: Okay, so I have removed the c[k] += b[i] according to an users suggestion and now it works perfectly fine. 编辑:好的,所以我已经根据用户的建议删除了c[k] += b[i] ,现在可以正常使用了。 But I still don't get it. 但是我还是不明白。 I mean I used the same line in the same program, the only difference being I didn't use the functions concept there and it worked perfectly fine. 我的意思是我在同一程序中使用了同一行,唯一的区别是我没有在那儿使用函数概念,并且工作得很好。 Here is the code: 这是代码:

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

int main() {
    char a[100], b[100];
    int k=0;

    printf("Please enter the string: \n");
    fgets(a,100,stdin);

    //To replace the '\n' at the end of the string inserted by fgets()
    for (int i = 0; i < strlen(a); ++i)
    {
        if(a[i] == '\n')
            a[i] = '\0';
    }

    for (int i = strlen(a)-1; i >=0 ; i--)
    {
        b[k] += a[i];
        k++;
    }

    if (strcmp(a,b) == 0)
    {
        printf("The entered string is palindrome!\n");
    }
    else
        printf("The entered string is not palindrome! \n");

    return 0;
}

Is there some concept behind it that I am unaware of? 我背后没有意识到一些概念吗? If so then please enlighten me. 如果是的话,请赐教。

You invoked undefined behavior by using values of an uninitialized variable having automatic storage duration, which is indeterminate. 您通过使用不确定的具有自动存储持续时间的未初始化变量的值来调用未定义的行为

Instead of adding in c[k] +=b[i]; 而不是添加c[k] +=b[i]; , just assign value from b as c[k] = b[i]; ,只需将b值分配为c[k] = b[i]; .

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

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