gpt4 book ai didi

c - 无法使回文检查起作用

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:13 26 4
gpt4 key购买 nike

我正在用 C 编写代码来检查输入的字符串是否为回文。到目前为止,我已经编写了反转输入字符串并将原始字符串与反转字符串进行比较的函数。不管输入是什么,输出总是“不是回文”。代码中某处有错误吗?还是我必须尝试不同的方法?代码如下。

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

void reverse(char *string);
int testStrings(char *a, char *b);

int main()
{
char string[100];
char string2[100];

printf("Please Enter a string:\n");
fgets(string, 100, stdin);
strcpy(string2, string);
reverse(string);

printf("Here's what you typed:%s\n",string2);
printf("Here's the Reversed:%s\n",string);
testStrings(string, string2);

return 0;
}

void reverse(char *s)
{
int c, i , j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
return;
}

int testStrings(char *a, char *b)
{
int i;
int length;
int c;

length = strlen(b) - 1;

for (i = 0; 1 <= length; i++) {
if (b[i] != a[i]) {
c = 1;
break;
}
}

if (c = 1) {
printf("The input is not a palindrome.\n");
} else {
printf("The inpiy is a palindrome.\n");
}

return 0;
}

最佳答案

函数 fgets 还包括字符串中对应于按下的 Enter 键的换行符。如果它存在于字符串中,您应该将其删除。例如

size_t n = strlen( string );

if ( n && string[n-1] == '\n' ) string[--n] = '\0';

考虑到最好从函数中排除打印语句testStrings 并将它们放在 main 中。该函数应返回 1 或 0,具体取决于字符串是否相等。并且不需要在函数内调用strlen

这个函数可以写得更简单,看起来像下面这样

int testStrings( const char *s1, const char *s2 )
{
while ( *s1 && *s1 == *s2 ) ++s1, ++s2;

return *s1 == *s2;
}

至于函数 reverse 那么最好这样写它,它会在目标字符串中以相反的顺序复制源字符串。例如

char * reverse_copy( char *s1, const char *s2 )
{
char *p = s1;
const char *q = s2 + strlen( s2 );

while ( q != s2 ) *p++ = *--q;

*p = '\0';

return s1;
}

函数调用看起来像

printf( "Here's what you typed:%s\n", string );
printf( "Here's the Reversed:%s\n", reverse_copy( string2, string ) );

int isPalindrome = testStrings( string, string2 );

// print stetements depending on whether the result is equal to 1 or 0

现在如果把所有的组合在一起你可以得到下面的程序

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

int testStrings( const char *s1, const char *s2 )
{
while ( *s1 && *s1 == *s2 ) ++s1, ++s2;

return *s1 == *s2;
}

char * reverse_copy( char *s1, const char *s2 )
{
char *p = s1;
const char *q = s2 + strlen( s2 );

while ( q != s2 ) *p++ = *--q;

*p = '\0';

return s1;
}

#define N 100

int main( void )
{
char string[N];
char string2[N];

printf( "Please Enter a string: " );
fgets( string, N, stdin );

size_t n = strlen( string );

if ( n && string[n-1] == '\n' ) string[--n] = '\0';

printf( "\nHere's what you typed: %s\n", string );
printf( "Here's the Reversed: %s\n", reverse_copy( string2, string ) );

if ( testStrings( string, string2 ) )
{
printf( "\nThe input is a palindrome.\n" );
}
else
{
printf( "\nThe input is not a palindrome.\n" );
}

return 0;
}

如果输入例如

123454321

那么输出就是

Please Enter a string: 123454321

Here's what you typed: 123454321
Here's the Reversed: 123454321

The input is a palindrome.

考虑到您可以在不使用第二个字符数组的情况下确定字符串是否为回文。在这种情况下,该函数可能如下所示

int isPalindrome( const char *s )
{
size_t n = strlen( s );
size_t i = 0;

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

return i == n / 2;
}

关于c - 无法使回文检查起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31170240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com