简体   繁体   English

弦回文功能

[英]String palindrome function

got a quick question. 有一个快速的问题。

So I've tried to design a palindrome function in C in one of my "beginner" programs. 因此,我尝试在我的“入门”程序之一中使用C设计回文功能。

For those unaware what palindrome is, basically it is a set of characters (usually a word, but could also be number - although in this case specifically word) that is spelled the same way backwards and forwards. 对于那些不知道什么回文的人来说,基本上是一组字符(通常是一个单词,但也可能是数字-尽管在这种情况下是单词),但它们前后拼写的方式相同。

Example of a palindrome - wow, lol, aaafaaa, ... 回文示例-哇,哈哈,aaaaaaaa,...

So you got the point. 所以你明白了。 So I began with my function 所以我从我的功能开始

int palindrome(char input[]){

So my presumption was, that ideally I'd want to run through the string with an index and compare it letter by letter. 所以我的假设是,理想情况下,我想遍历带有索引的字符串,然后逐个字母比较它。

int palindrome(char input[]){
 int start = 0, length = 0, end;
 /* Until we reach end of the word */
 while (input[start++] != '\0'){
   length++;

   for(start = 0, end = length - 1; start = length / 2; end--){
   /*If they do not match, return 0 */
     if (input[start] != input[end]){
        return 0;
        break;
     }
   }
 }
return 1;
}

That's how my palindrome function looks like. 那就是我的回文功能的样子。 Now I only want to check user input from standard stdin. 现在,我只想检查来自标准stdin的用户输入。

So my main function looks like this 所以我的主要功能是这样的

int main(){
char uInput[30];

/* Welcome user */
printf("Hello, please enter some text \n);
scanf("%29s", uInput);

if palindrome(uInput){
printf("The word: %s is a palindrome \n", uInput);
}

else {
printf("The word: %s is not a palindrome \n", uInput);
}

return 0;
}

So pretty straightforward code there, unfortunately, my results are 如此简单的代码很不幸,我的结果是

"The word (word) is not a palindrome" “单词(单词)不是回文”

regardless of whether it is a palindrome or not. 不管它是否是回文。

So there probably is something wrong with my function altogether. 因此,我的功能可能完全出问题了。 Also I am aware that this could be done through other libraries such as string.h and others, but I personally would prefer to do it this way as a form of exercise rather than using predefined functions. 我也知道可以通过其他库(例如string.h和其他库)来完成此操作,但是我个人更喜欢以这种方式将其作为一种练习形式,而不是使用预定义的函数。

So yeah, I've got a strong suspicion I'm not using my returns correctly in the function, but I'm not exactly sure what is the actual error with them. 所以,是的,我很怀疑我没有在函数中正确使用返回值,但是我不确定它们的实际错误是什么。

Your idea is correct, there are just a few typos and omittings. 您的想法是正确的,只有一些错别字和遗漏之处。

Your for loop is wrong, it should be: 您的for循环是错误的,应该是:

for(start = 0, end = length - 1; start != length / 2; start++, end--)

instead of: 代替:

for(start = 0, end = length - 1; start = length / 2; end--)

And your while loop includes the whole for loop as well which is nonsense. 而且您的while循环也包括整个for循环,这是胡说八道。 It should be just: 应该只是:

while (input[start++] != '\0')
  length++;

and the } before return 1 should be removed. 并删除return 1之前的}

And int start = 0 is not necessary as you initialize start at the beginning of the for loop anyway; 而且int start = 0并不是必需的,因为无论如何您要在for循环的start处初始化start int start is enough. int start就足够了。 But this is not really an error. 但这并不是真正的错误。

There are multiple errors in the palindrome function 回文功能存在多个错误

We can do with a single loop and not a loop inside a loop. 我们可以使用一个循环而不是一个循环内部的循环。 Also note the terminating condition start != (length / 2) of for loop and increment of both start and end . 还要注意for循环的终止条件start != (length / 2)以及startend增量。

There are also some compilation errors fixed. 还修复了一些编译错误。 The complete code is below. 完整的代码如下。

#include <stdio.h>
int palindrome(char input[]){
 int start = 0, length = 0, end;

 /* Until we reach end of the word */
 while (input[length] != '\0')
   length++;

 for(start = 0, end = length - 1; start != (length / 2); start++, end--){
 /*If they do not match, return 0 */
     if (input[start] != input[end]){
        return 0;
     }
 }

 return 1;
}

int main(){
char uInput[30];

/* Welcome user */
printf("Hello, please enter some text \n");
scanf("%29s", uInput);

if (palindrome(uInput)){
printf("The word: %s is a palindrome \n", uInput);
}

else {
printf("The word: %s is not a palindrome \n", uInput);
}
return 0;
}

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

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