回文程序不工作请检查附件代码和输出
pallindrome program not working please check attached the code and the output
我已经尝试制作一个程序来在纸上手动检查回文串,它应该可以工作
但事实并非如此。
我附上了输出代码
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
char str[100];
int i,l,k;
printf("type the desired to string to check pallindrome\n");
gets(str);
l=strlen(str);
printf("%d\n", l);
k=l-1;
char temp[100];
for(i=0;i<l;i++)
{
temp[k]=str[i];
--k;
}
if(strcmp(temp, str)==0)
{
printf("pallindrome\n");
}
else
{
printf("not a pallindrome\n");
}
}
这是输出
[1]: https://i.stack.imgur.com/dERFQ.png
您需要在 for
循环之后的 temp
末尾附加 NUL 终止符:
for(i=0;i<l;i++)
{
temp[k]=str[i];
--k;
}
temp[i] = '[=10=]';
否则,strcmp
函数会导致UB。
对于根据 C 标准的初学者,不带参数的函数 main 应声明为
int main( void )
数组 temp
不包含字符串,因为您忘记在其后附加终止零字符“\0”。
所以strcmp
的调用
if(strcmp(temp, str)==0)
导致未定义的行为。
此外,函数 gets
不安全且不受 C 标准支持。而是使用函数 fgets
.
还要检查一个字符串是否是回文,不需要声明辅助数组。
代码看起来像
printf("type the desired to string to check pallindrome\n");
fgets(str, sizeof( str ), stdin );
str[strcspn( str, "\n" )] = '[=12=]'; // to remove the new line character '\n'
size_t n = strlen( str );
printf( "%zu\n", n );
size_t i = 0;
while ( i < n / 2 && str[i] == str[n-i-1] ) ++i;
if( i == n / 2 )
{
printf("pallindrome\n");
}
else
{
printf("not a pallindrome\n");
}
您可以编写一个单独的函数来检查字符串是否为回文。
给你。
#include <stdio.h>
#include <string.h>
int is_palindrome( const char *s )
{
size_t n = strlen( s );
size_t i = 0;
while ( i < n / 2 && s[i] == s[n-i-1] ) ++i;
return i == n / 2;
}
int main(void)
{
enum { N = 100 };
char s[N];
printf( "Type a desired string to check whether it is a palindrome: " );
fgets( s, sizeof( s ), stdin );
s[ strcspn( s, "\n" ) ] = '[=13=]';
if ( is_palindrome( s ) )
{
printf( "\"%s\" is a palindrome.\n", s );
}
else
{
printf( "\"%s\" is not a palindrome.\n", s );
}
return 0;
}
程序输出可能看起来像
Type a desired string to check whether it is a palindrome: abcdedcba
"abcdedcba" is a palindrome
我已经尝试制作一个程序来在纸上手动检查回文串,它应该可以工作 但事实并非如此。 我附上了输出代码
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
char str[100];
int i,l,k;
printf("type the desired to string to check pallindrome\n");
gets(str);
l=strlen(str);
printf("%d\n", l);
k=l-1;
char temp[100];
for(i=0;i<l;i++)
{
temp[k]=str[i];
--k;
}
if(strcmp(temp, str)==0)
{
printf("pallindrome\n");
}
else
{
printf("not a pallindrome\n");
}
}
这是输出
[1]: https://i.stack.imgur.com/dERFQ.png
您需要在 for
循环之后的 temp
末尾附加 NUL 终止符:
for(i=0;i<l;i++)
{
temp[k]=str[i];
--k;
}
temp[i] = '[=10=]';
否则,strcmp
函数会导致UB。
对于根据 C 标准的初学者,不带参数的函数 main 应声明为
int main( void )
数组 temp
不包含字符串,因为您忘记在其后附加终止零字符“\0”。
所以strcmp
if(strcmp(temp, str)==0)
导致未定义的行为。
此外,函数 gets
不安全且不受 C 标准支持。而是使用函数 fgets
.
还要检查一个字符串是否是回文,不需要声明辅助数组。
代码看起来像
printf("type the desired to string to check pallindrome\n");
fgets(str, sizeof( str ), stdin );
str[strcspn( str, "\n" )] = '[=12=]'; // to remove the new line character '\n'
size_t n = strlen( str );
printf( "%zu\n", n );
size_t i = 0;
while ( i < n / 2 && str[i] == str[n-i-1] ) ++i;
if( i == n / 2 )
{
printf("pallindrome\n");
}
else
{
printf("not a pallindrome\n");
}
您可以编写一个单独的函数来检查字符串是否为回文。
给你。
#include <stdio.h>
#include <string.h>
int is_palindrome( const char *s )
{
size_t n = strlen( s );
size_t i = 0;
while ( i < n / 2 && s[i] == s[n-i-1] ) ++i;
return i == n / 2;
}
int main(void)
{
enum { N = 100 };
char s[N];
printf( "Type a desired string to check whether it is a palindrome: " );
fgets( s, sizeof( s ), stdin );
s[ strcspn( s, "\n" ) ] = '[=13=]';
if ( is_palindrome( s ) )
{
printf( "\"%s\" is a palindrome.\n", s );
}
else
{
printf( "\"%s\" is not a palindrome.\n", s );
}
return 0;
}
程序输出可能看起来像
Type a desired string to check whether it is a palindrome: abcdedcba
"abcdedcba" is a palindrome