c - C Lang:使用指针和数组检查是否回文
问题描述
我一直在尝试用指针对此进行编码,我能够让第一个 for 循环工作,但我无法让另一个 for 循环使用指针工作。程序将忽略所有不是字母的字符并使用指针来代替整数以跟踪数组中的位置。我需要指针帮助来跟踪数组中的位置。
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#define N 50
int main()
{
int i, j, k =0,*count; char ch, msg[N], msg1[N]; bool flag = true;
char *p;
count = &k;
// Getting user-defined input
printf("Enter a massage: ");
ch = getchar();
while(ch != '\n' || *count == N)
{
msg[(*count)++] = ch;
ch = getchar();
} // end of while loop
// Printing user-defined input
printf("User-defined input: ");
for(p = msg; p < msg+*count; p++)
printf("%c", *p);
printf("\nNumber of characters is %d\n", *count);
// Sanitiazation of the msg array (i.e., removing special characters)
i = 0;
for(j = 0; msg[j]; j++) {
if(isalpha(msg[j]))
msg1[i++] = tolower(msg[j]);
} // end of for loop
// Palindrome test
for(j = 0; msg1[j]; j++) {
if(msg1[j] != msg1[i-j-1]) {
flag = false;
continue;
}
} // end of for loop
// Printing final decision
if(flag)
printf("Palindrome\n");
else
printf("Not palindrome\n");
return 0;
} // end of main
解决方案
推荐阅读
- html - 右侧的 div 宽度受限
- javascript - 轴刻度未显示在我的 d3 图上
- unity3d - 当对象的 z 旋转超过限制时函数不会触发
- python - BeautifulSoup 没有显示所有的元关键字。只有部分关键字存在
- node.js - .on("close") 即使用户没有关闭也会运行
- azure - Microsoft 文件服务器升级后 Azure 应用服务未重新启动
- windows - 如何在此 PowerShell 代码中添加异常处理?
- html - Jekyll:点击特定类别并进入新页面后,它没有显示该类别的帖子,而我有一些适合
- c# - 如何通过javascript在c#中第一次播放嵌入式YouTube(使用Gecko浏览器)?
- node.js - 什么是重定向 URI,它应该是什么?