C语言编程求回文的程序,我是初学者,以下程序看不懂,求高手解释。

C语言编程求回文的程序,我是初学者,以下程序看不懂,求高手解释。,第1张

回文数的程序:回文数是从宏销山右向左读和蔽中从左向右读都是一样的。比如15651,

1221等。这个程序是验证用户输入的一个四位数是不是回文数。但是程序好像有错误:

举个例子:用户输入5885。先分离数字斗贺,a,b,c,d分别等于5,8,8,5。然后判断a是否等于d且b是否等于c。如果条件满足则是回文数,否则不是。

首先我对你的 "并且当输入的字符串第一个字符为#时,输入为空时,不输出。" 这句话比较费解, 不明白你的意思!

你把这说清楚了我再补充回答你~

我写了个参考代码给你参考, 首先是输入你要谈差判断的字符串的个数, 然后再依次输入所有的字符串, 最后判断输入的所有字符串是否是"回文"! 因为不理解你那句话, 所以暂时没做什么空和什么"#"处理.

详细c代码:

#include <stdio.h>

#include <string.h>

#define STR_LEN 128

#define STR_NUM 64

int main()

{

int i = 0, j = 0, n = 0

int len = 0

char *start = NULL

char *end = NULL

char str[STR_NUM][STR_LEN] = {0}

printf("Please input the number of string: \n")

scanf("%d", &n)

printf("Please input all the string: \n")

for (i = 0i <ni++)

{

scanf("%s", str[i])

}

for (j = 0j <nj++)

{

start = str[j]

len = strlen(str[j])

end = str[j] + len - 1

while (start - end <= 0)

{

if (*start++ != *end--)

{

break

}

}

if (start >end)

{

printf("yes\n")

}

else

{

printf("no\n")

}

}

return 0

}

例子,运行后:

Please input the number of string:

4

Please input all the string:

aba

112ds

madam

xyzyx

yes

no

yes

yes

Press any key to continue

补充回答:

大森兄概明白你的意思,我会此侍袭把例子贴上, 若不符合你的要求我再帮修改!

详细代码如下:

#include <stdio.h>

#include <string.h>

#define STR_LEN 128

#define STR_NUM 64

int main()

{

int i = 0

int len = 0

int str_count = 0

char *start = NULL

char *end = NULL

char str[STR_NUM][STR_LEN] = {0}

printf("Please input all the string: \n")

while (1)

{

scanf("%s", str[str_count])

if (str[str_count][0] == '#')

{

break

}

else

{

str_count++

continue

}

}

for (i = 0i <str_counti++)

{

start = str[i]

len = strlen(str[i])

end = str[i] + len - 1

while (start - end <= 0)

{

if (*start++ != *end--)

{

break

}

}

if (start >end)

{

printf("yes\n")

}

else

{

printf("no\n")

}

}

return 0

}

运行实例1:

Please input all the string:

xyzyx

adghf

#

yes

no

Press any key to continue

运行实例2:

Please input all the string:

1232ss

sakljfkla

333dafs

aba

ee3

xyzyx

dfj222

madam

111$111

slsl33

#

no

no

no

yes

no

yes

no

yes

yes

no

Press any key to continue

1、首先打开vc6.0,新建一个控制台项目,添加头文件。

2、然后我们根据图示代码添加main主毁哗函数。

3、然后我们根据图示代码定义6个long型变量。

4、然后我们根据图示代码使用scanf给input赋值。

5、然后我们根据图示代码分解个位、百位虚激、千位、万位。

6、差余袜然后我们根据图示代码使用if判断。

7、然后我们根据图示代码运行程序,看看结果无误。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12454902.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存