简体   繁体   English

在C中的回文取景器?

[英]Palindrome finder in C?

I'm trying to make a palindrome finder in C and I don't know where it is going wrong, no matter what I get the output false on the 2 different ways that I have tried to code this. 我正在尝试使用C创建一个回文查找器,无论我尝试编写此代码的2种不同方式使输出为假,我都不知道它出了什么问题。 I have only just started C (in the past week) so if you could explain things simply that'd be great, thanks! 我才刚刚开始C(在过去的一周中),所以如果您能简单地解释一下,那就太好了,谢谢!

//way1
#include <stdio.h>

int read_char() { return getchar(); }
void read_string(char* s, int size) { fgets(s, size, stdin); }

void print_char(int c)     { putchar(c); }   
void print_string(char* s) { printf("%s", s); }


int is_palin(char word[]) {

  int m = 0;
  int arr_len = sizeof(word) / sizeof(char); //change to char_index
  int n = arr_len;
  int t = 1;

  if(n % 2 != 0) {
    for (m=0; m < ((n-1)/2); m++) {
      if(word[m] != word[n-m-2]) {
        t = 0;
      }
      else {
        t = 1;
      }
    }
  }
  else {
    for (m=0; m < (n/2)-1; m++) {
      if(word[m] != word[n-m-2]) {
        t = 0;
      }
      else {
        t = 1;
      }
    }
  }

  if(t == 1) {
    return 1;
  }
  else {
    return 0;
  }
}

int main(void) {
  char word[6] = "civic";
  int arr_len = sizeof(word)/sizeof(char);

  if (is_palin(word) == 1) {
    printf("is palin\n");
  }
  else {
    printf("is not palin\n");
  }

  printf(word);
  printf("\n");
  printf("%d\n", arr_len);
  return 0;
}

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

//way2
#include <stdio.h>

int read_char() { return getchar(); }
void read_string(char* s, int size) { fgets(s, size, stdin); }

void print_char(int c)     { putchar(c); }   
void print_string(char* s) { printf("%s", s); }


int is_palin(char word[]) {
  int m = 1;
  int input_length = sizeof(word);
  int j = input_length-1;
  int i = 0;

  for(i=0; i <= j; i++) {
    if(word[i] != word[j]) {
      m = 0;
      j--;
    }
  }

  if(m == 1) {
    return 1;
  }
  else {
    return 0;
  }
}


int main(void) {
  char word[6] = "civic";
  int input_length = sizeof(word);

  if (is_palin(word) == 1) {
    printf("is palin\n");
  }
  else {
    printf("is not palin\n");
  }

  printf(word);
  printf("\n");
  printf("%d\n", input_length);
  return 0;
}

Please try this, it works fine. 请尝试此,它工作正常。

   #include <stdio.h>

    int main(  )
    {
      int flag = 0;
      int length = 0;
      int len2 = 0;
      int i = 0;
      char name[130];
      char p[130];
      char q[130];

      printf( "please enter a name or sentence\n" );
      scanf( "%[^\n]", name );

      length = strlen( name );
      len2 = length;
      strcpy( p, name );
      memset( q, '.', length ); // handy to debug comparaison
      q[length] = '\0';

      for ( i = 0; i < length; i++ )
      {
        q[--len2] = p[i];
      }

      printf( "\n p==%s", p );
      printf( "\n q==%s", q );
      getchar(  );

      if ( !strcmp( p, q ) )
        flag = 1;

      if ( flag == 1 )
        printf( "\npalindrome\n" );
      else
        printf( "\nnot a palindrome\n" );

      return 0;
    }

Take a look at this code, that's how I have implemented it (remember to #include <stdbool.h> or it will not work): 看一下这段代码,这就是我实现它的方式(请记住#include <stdbool.h>否则将无法正常工作):

for(i = 0; i < string_length; i++)
    {
            if(sentence[i] == sentence[string_lenght-1-i])
                    palindrome = true;
            else
            {
                    palindrome = false;
                    break;
            }
    }

Doing that it will check if your sentence is palindrome and, at the first occurence this is not true it will break the for loop. 这样做将检查您的句子是否是回文,并且在第一次出现时不正确,这将中断for循环。 You can use something like 您可以使用类似

if(palindrome)
     printf(..);
else
     printf(..);

for a simple prompt for the user. 为用户提供简单提示。

Example : 范例:

radar is palindrome 雷达是回文

abba is palindrome abba是回文

abcabc is not palindrome abcabc不是回文

Please , pay attention to the fact that 请注意以下事实

Abba 阿巴

is not recognized as a palindrome due to the fact that ' A ' and 'a' have different ASCII codes : 由于“ A”和“ a”具有不同的ASCII码,因此不能识别为回文式:

'A' has the value of 65 “ A”的值为65

'a' has the value of 97 according to the ASCII table . 根据ASCII表, “ a”的值为97。 You can find out more here . 你可以在这里找到更多。

You can avoid this issue trasforming all the characters of the string to lower case characters. 您可以避免此问题将字符串的所有字符转换为小写字符。 You can do this including the <ctype.h> library and calling the function int tolower(int c); 您可以包括<ctype.h>库并调用int tolower(int c);函数来完成此操作int tolower(int c); like that : 像那样 :

 for ( ; *p; ++p) *p = tolower(*p); 

or 要么

 for(int i = 0; str[i]; i++){ str[i] = tolower(str[i]); } 

Code by Earlz , take a look at this Q&A to look deeper into that. Earlz编写的代码,请查看此问答,以更深入地了解这一问题。

EDIT : I made a simple program to do this, see if it can help you 编辑:我做了一个简单的程序来执行此操作,看它是否可以帮助您

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>

void LowerCharacters(char *word, int word_lenth);

int main(void){

    char *word = (char *) malloc(10);
    bool palindrome = false;

    if(word == 0)
    {
        printf("\nERROR : Out of memory.\n\n");
        return 1;
    }

    printf("\nEnter a word to check if it is palindrome or not : ");
    scanf("%s", word);

    int word_length = strlen(word);

    LowerCharacters(word,word_length);

    for(int i = 0; i < word_length; i++)
    {
        if(word[i] == word[word_length-1-i])
            palindrome = true;
        else
        {
            palindrome = false;
            break;
        }
    }

    palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);

    free(word);

return 0;

}

void LowerCharacters(char *word, int word_length){

    for(int i = 0; i < word_length; i++)
        word[i] = tolower(word[i]);
}

Input : 输入:

Enter a word to check if it is palindrome or not : RadaR 输入一个单词以检查它是否是回文:RadaR

Output : 输出:

The word radar is palindrome. 雷达一词是回文。

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

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