21xrx.com
2024-04-24 01:10:43 Wednesday
登录
文章检索 我的文章 写文章
C 中的回文
2021-07-07 08:49:53 深夜i     --     --
C

用于检查字符串或数字是否为回文的 C 程序。 回文字符串是向后和向前读取相同的字符串。 它可以是奇数或偶数长度。

回文数是与它的倒数相等的数。

检查回文字符串的算法

 

  1. 将输入字符串复制到新字符串中(strcpy 函数)。
  2. 反转它(我们建议不要使用 strrev 函数,因为它与 C99 不兼容)。
  3. 将其与原始字符串(strcmp 函数)进行比较。
  4. 如果它们都具有相同的字符序列,即它们相同,则该字符串是回文,否则不是。

不使用字符串函数检查回文的 C 程序。 一些回文字符串是:“C”、“CC”、“女士”、“ab121ba”、“C++&&++C”。

 

C语言回文程序

#include <stdio.h>
#include <string.h>


int main()
{
  char a[100], b[100];

  printf("Enter a string to check if it's a palindrome\n");
  gets(a);

  strcpy(b, a);  // Copying input string
  strrev(b);  // Reversing the string

  if (strcmp(a, b) == 0)  // Comparing input string with the reverse string
    printf("The string is a palindrome.\n");
  else
    printf("The string isn't a palindrome.\n");

  return 0;
}

 

C 回文程序的输出:

下载回文程序。

不使用字符串函数的回文 C 程序

#include <stdio.h>


int main()
{
  char text[100];
  int begin, middle, end, length = 0;

  gets(text);

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

  end = length - 1;
  middle = length/2;

  for (begin = 0; begin < middle; begin++)
  {
    if (text[begin] != text[end])
    {
      printf("Not a palindrome.\n");
      break;
    }
    end--;
  }
  if (begin == middle)
    printf("Palindrome.\n");

  return 0;
}

C中的回文数

#include <stdio.h>


int main()
{
   int n, r = 0, t;

   printf("Enter an integer to check if it's palindrome or not\n");
   scanf("%d", &n);

   t = n;

   while (t != 0)
   {
      r = r * 10;
      r = r + t%10;
      t = t/10;
   }

   if (n == r)
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d isn't a palindrome number.\n", n);

   return 0;
}

使用函数和指针的 C 回文程序

#include <stdio.h>
 
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
 
int main()
{
   char string[100];
   int result;
 
   printf("Input a string\n");
   gets(string);
 
   result = is_palindrome(string);
 
   if (result == 1)
      printf("\"%s\" is a palindrome string.\n", string);
   else
      printf("\"%s\" isn't a palindrome string.\n", string);
 
   return 0;
}
 
int is_palindrome(char *string)
{
   int check, length;
   char *reverse;
 
   length = string_length(string);    
   reverse = (char*)malloc(length+1);    
 
   copy_string(reverse, string);
   reverse_string(reverse);
 
   check = compare_string(string, reverse);
 
   free(reverse);
 
   if (check == 0)
      return 1;
   else
      return 0;
}
 
int string_length(char *string)
{
   int length = 0;  
 
   while(*string)
   {
      length++;
      string++;
   }
 
   return length;
}
 
void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}
 
void reverse_string(char *string)
{
   int length, c;
   char *begin, *end, temp;
 
   length = string_length(string);
 
   begin = string;
   end = string;
 
   for (c = 0; c < (length - 1); c++)
       end++;
 
   for (c = 0; c < length/2; c++)
   {        
      temp = *end;
      *end = *begin;
      *begin = temp;
 
      begin++;
      end--;
   }
}
 
int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if (*first == '\0' || *second == '\0')
         break;
 
      first++;
      second++;
   }
   if (*first == '\0' && *second == '\0')
      return 0;
   else
      return -1;
}

您也可以在不使用指针的情况下编写程序。 回文字符串可以是单个单词、短语或句子。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复