指针回文(有人可以通过简单的方法逐步解释以下代码...非常感谢!) [英] pointer palindrome(can someone please explain the following code step by step and in a easy way...thanks a lot!!!)
本文介绍了指针回文(有人可以通过简单的方法逐步解释以下代码...非常感谢!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <ctype.h>
#define STR_LEN 80
int main()
{
char s[STR_LEN+1];
int i = 0;
char *left;
char *right;
char *end;
for (i = 0; i< = STR_LEN; i++)
{
s[i] = ' ';
}
printf("Enter: ");
fgets(s, STR_LEN+1, stdin);
left = &s[0];
end = right = &s[STR_LEN];
while (left<right)
{
while ((!((*left>=65)&&(*left<=90)))&&
(!((*left>=97)&&(*left<=122)))&&
(left<end))
{
left++;
}
while ((!((*right>=65)&&(*right<=90)))&&
(!((*right>=97)&&(*right<=122)))&&
(right>s))
{
right--;
}
if (left == end)
{
break;
}
if (tolower(*left) != tolower(*right))
{
printf("\nNo Palindrome!\n\n");
return 0;
}
left++;
right--;
}
printf("\nPalindrome!\n\n");
return 0;
}
推荐答案
它从字符数组的每个末端开始,以查找字符匹配项.给定字符串"abccba",它将比较位置0和5、1、4和2和3处的字符是否相同.如果是这样,那就是回文.如果碰巧不匹配,那就不是回文.从技术上讲,它不应该区分大小写,但这只是实现的问题.
顺便说一句,我对格式进行了一些更改,以便更轻松地了解其工作原理.
It''s starting at each end of the character array, looking for character matches. Given the string "abccba", it''s comparing the character at position 0 and 5, 1 and 4, and 2 and 3 to see if they''re the same. If they are, it''s a palindrome. If it runs into a pair that doesn''t match, it''s not a palindrome. Technically, it should not be case-sensitive, but that''s a matter of implementation.
BTW, I change the formatting a little so it''s easier to see how it''s working.
这篇关于指针回文(有人可以通过简单的方法逐步解释以下代码...非常感谢!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文