[英]Checking for palindrome string in c
I am accepting a string as a command line argument. 我接受一个字符串作为命令行参数。 I want to check whether the inputted string is a palindrome or not and print the result. 我想检查输入的字符串是否是回文并打印结果。 I have written the following code. 我写了以下代码。 But its displaying the result 'not palindrome' for all inputs. 但它显示所有输入的结果“不是回文”。
#include<stdio.h>
#include<string.h>
int main(int argc, char argv[20]) {
int i;
int l = strlen(argv);
char str[20];
bzero(str, 20);
for(i=0; i<l; i++)
{
str[i] = argv[i+2];
}
int flag;
int len = strlen(str);
for(i=0; i< len/2; i++)
{
if(str[i] == str[len - (i+2)])
{
flag = 0;
}
else
{
flag = 1;
break;
}
}
if(flag == 0)
printf("Palindrome\n");
else
printf("Not a palindrome\n");
}
You could do it in a K&R-style by having two offset iterators in a for
-loop: 您可以通过在for
-loop中使用两个偏移迭代器来以K&R样式执行此操作:
#include <stdio.h>
#include <string.h>
#include <assert.h>
int main(int argc, char *argv[]) {
assert(argc != 1);
char *text = argv[1];
int len = strlen(text);
int is_palindrome = 1;
int i, j;
for(i = 0, j = len - 1; i < len / 2; i++, j--) {
if(text[i] != text[j]) {
is_palindrome = 0;
break;
}
}
printf("%s a palindrome.\n", is_palindrome ? "is" : "isn't");
return(0);
}
Changes from original: 原始变更:
Updated based on comments: 根据评论更新:
int is_palindrome(const char *s)
{
const char *t = s + strlen(s);
while (s<t && *s==*--t) s++;
return s>=t;
}
And since the OP wants a version that's not so heavy on pointers: 而且由于OP需要一个对指针不那么重的版本:
int is_palindrome(const char *s)
{
size_t i=0, j = strlen(s);
while (i<j && s[i]==s[--j]) i++;
return i>=j;
}
For reference, here's the original buggy version: 作为参考,这是原始的错误版本:
int is_palindrome(const char *s)
{
const char *t = s + strlen(s) - 1;
while (s<t && *s++==*t--);
return s>=t;
}
For one thing, your signature for main
is off. 首先,你的main
签名是关闭的。 It should be int main(int argc, char** argv)
or int main(int argc, char * argv[])
. 它应该是int main(int argc, char** argv)
或int main(int argc, char * argv[])
。 You're treating a pointer to a string as if it were a string. 您正在处理指向字符串的指针,就像它是一个字符串一样。
When you've changed that, the string you want should be in argv[1]
(since argv[0]
is some representation of the program name). 当你改变它时,你想要的字符串应该在argv[1]
(因为argv[0]
是程序名称的一些表示)。
There's a good case for using pointers rather than indexes for this: 有一个很好的例子,使用指针而不是索引:
int is_palindrome(const char *s) {
const char *end = s + strlen(s);
while (end > s) {
--end;
if (*end != *s) return 0;
++s;
}
return 1;
}
If you like short, confusing code, you can re-write that: 如果你喜欢简短,令人困惑的代码,你可以重写:
int is_palindrome(const char *s) {
const char *end = s + strlen(s);
while (end > s) if (*(--end) != *(s++)) return 0;
return 1;
}
argv
isn't a string, it's an array of strings, one for the program name and then one for each argument (usually space-separated in a command line). argv
不是字符串,它是一个字符串数组,一个用于程序名,另一个用于每个参数(通常在命令行中以空格分隔)。 So to test if the first argument is a palindrome, you're interested in argv[1]. 因此,为了测试第一个参数是否是回文,你对argv [1]感兴趣。
int main(int argc, char **argv) {
if (argc != 2) {
printf("usage: %s <string>\n", argv[0]); // or something
return 1;
}
if (is_palindrome(argv[1])) {
printf("Palindrome\n");
} else {
printf("Not a Palindrome\n");
}
}
The first loop doesn't make sense. 第一个循环没有意义。 Copying the string to another doesn't make sense. 将字符串复制到另一个字符串没有意义。
Just do it and adjust the index: 只需这样做并调整索引:
#include<stdio.h>
#include<string.h>
int main(int argc, char **argv) {
int i;
char * str = argv[1];
int flag;
int len = strlen(str);
for(i=0; i< (len+1)/2; i++)
{
printf("DEBUG: Comparing %c %c\n",str[i], str[len - (i+1)]);
if(str[i] == str[len - (i+1)])
{
flag = 0;
}
else
{
flag = 1;
break;
}
}
if(flag == 0)
printf("Palindrome\n");
else
printf("Not a palindrome\n");
}
No pointers (except the one use for making a copy of the original string). 没有指针(除了用于制作原始字符串副本的指针)。
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] )
{
char *s2;
if ( argc != 2 )
return ( 1 ); // not properly invoked
if ( (s2 = strdup( argv[1] )) == NULL )
return ( 2 ); // failed (not likely)
printf( "\"%s\" %s a palindrome.\n", argv[1], strcmp( argv[1], strrev( s2 ) ) ? "is not" : "is" );
free( s2 );
return ( 0 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.