- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用 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/
我陷入了一项作业,很大程度上是因为我的教科书和类 Material 中极度缺乏示例,甚至是相关图表。 我按照自己的方式构建程序的原因是因为我需要使用 4 个方法:一个执行所有其他方法的主方法、一个检索
我尝试使用列表编写 Prolog 程序。但是,我必须使用 差异列表和输出应该是: 列表的第 i 个元素与列表的第 (n-i+1) 个元素相同,n 是列表的长度。例如,[a,X,c,b,Y]应该给 X
我一直在做Python学习任务,我遇到了这个任务,我必须读取一个包含几个单词的文件,并且一行是否是回文(向后写时相同:哈哈>哈哈)所以我尝试使用此代码,但它不会在终端上打印任何内容: with ope
我是 C# 的新手,我正在制作一个小应用程序来检查控制台的输入是否为回文。我自己走了很远,但我遇到了一个错误。 代码: class Program { static void Main(str
我想求出3位数字中最大的回文数。这是我的代码: for (int start = 100; start biggest) { biggest = temp;
我试着写了一个叫做 is_palidrome_v3(s) 的方法来比较单词的第一个和最后一个字母,然后是第二个和最后一个第二个字母,直到这对不一样,最后检查索引是否 >= len(s )//2 def
我正在编写一个函数来反转字符串并检查该字符串是否为回文。当我用像“abba”这样明显的回文来测试函数时,函数说它不是回文。此外,正向字符串和反向字符串的字符串长度也不同! #include #inc
我有一个纯 ASCII 字符串,它要么已经是一个回文串,要么可以通过删除一个字符变成回文串。我需要确定它是否已经是回文,如果不是,我需要找到需要删除的字符的索引。比如字符串是'aaba',那么去掉第一
美好的一天!我们的老师要求我们确定一个单词或一系列数字是否是回文或不使用堆栈。我已经做完了。但是我想多练习,所以现在我正在尝试通过删除空格和其他不相关的字符来确定一个句子是否是回文(注意:不再是我的作
回文是可以从任一方向以相同方式阅读的单词、短语、数字或其他单位序列。编写一个函数来确定给定的单词或数字是否为回文。 示例 >>> isPalindrome("") False >>> isPalind
with open('33.txt') as text: for line in text: line2 = line[:][::-1] if line ==
我正在做一些 python 在线教程,但我遇到了一个练习:A palindrome is a word which was spelled forwards as backwards.例如,单词 赛车
所以几个月前我在参加编程面试时,由于某种原因这个问题让我绊倒了。我可以想到几个解决方案,但其中大多数似乎效率极低。虽然多年来我一直以某种身份进行编程,但我目前正在大学攻读 CS 学位,所以我的引用点可
我正在尝试解决以下面试练习题: A k-palindrome is a string which transforms into a palindrome on removing at most k
无论我输入什么,我的代码都会打印出“true”。我的代码没有显示任何错误,我输入 56 作为输入,它仍然打印“true”。我真的很难弄清楚为什么。另外,我对 Java 和 Stack Overflow
我正在尝试弄清楚如何在给定的特定单词中找到回文。即 - banana 回文将是 anana。我有一个功能可以让我检查整个单词是否是一个,但我不知道如何检查单词的任何部分是否是回文。关于什么可以解决这个
/* 但如果我将第 13 行更改为“else return palindrome(s,++f, --l);”然后代码运行良好。这是什么原因?*/ #include #include #include
我想知道为什么结果不正确。如果我键入字符串来测试该字符串是否为回文,每当我尝试时,结果都是一样的。 例如,如果我输入“爸爸”进行测试,这表明它不是回文。但是,如果我输入“god”,它仍然告诉我这不是回
完全披露:这是一项作业,所以请不要发布实际的代码解决方案! 我有一个作业要求我从用户那里获取一个字符串并将其传递到堆栈和队列中,然后使用这两者来比较字符以确定该字符串是否为回文。我已经编写了程序,但是
如何在 C++ 中创建回文函数?我正在使用 2 种函数类型(bool 和 void)。到目前为止,这是我的代码(我真的很感激任何帮助,为什么我的代码不起作用?)谢谢! #include #inclu
我是一名优秀的程序员,十分优秀!