编写函数int ispalin(char *string),对字符串string是否回文进行判断,是回文,返回标志flag为1,不是回文,返回标志flag为0
时间: 2023-03-19 11:25:40 浏览: 170
函数int ispalin(char *string)的实现如下:
int ispalin(char *string){
int len = strlen(string);
int flag = 1;
for(int i=; i<len/2; i++){
if(string[i] != string[len-i-1]){
flag = ;
break;
}
}
return flag;
}
该函数首先获取字符串的长度len,然后遍历字符串的前一半,判断对应位置的字符是否与后一半对应位置的字符相同,如果不同,则将标志flag设为,表示不是回文。最后返回标志flag的值。
相关问题
c编写一个程序,输入一个字符串,判断该字符串是否为回文字符串。回文字符串指顺序与倒序相同的字符串,例如"level"就是一个回文字符串。
以下是一个用C语言编写的判断回文字符串的程序:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, len, flag = 1;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
for (i = 0, j = len - 1; i < len / 2; i++, j--) {
if (str[i] != str[j]) {
flag = 0;
break;
}
}
if (flag)
printf("%s是回文字符串\n", str);
else
printf("%s不是回文字符串\n", str);
return 0;
}
```
在程序中,首先定义了一个字符数组`str`和三个整型变量`i`、`j`和`len`。然后,通过`scanf`函数获取用户输入的字符串,并使用`strlen`函数获取该字符串的长度。
接下来,使用`for`循环遍历字符串的前半部分(因为回文字符串的后半部分和前半部分相同),并判断前半部分的字符是否与后半部分的字符相同。如果存在不同的字符,将`flag`标志设为0,表示该字符串不是回文字符串,并跳出循环。
最后,根据`flag`标志的值输出判断结果。如果`flag`为1,则该字符串是回文字符串;如果`flag`为0,则该字符串不是回文字符串。
c语言判断字符串是否是回文数,不能用string函数
好的,以下是一个判断字符串是否为回文数的C语言代码,不使用任何string函数:
```
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];
int i, len, flag = 0;
printf("请输入字符串:");
scanf("%s", str);
len = strlen(str);
for(i = 0; i < len/2; i++)
{
if(str[i] != str[len-i-1])
{
flag = 1;
break;
}
}
if(flag == 1)
{
printf("该字符串不是回文数\n");
}
else
{
printf("该字符串是回文数\n");
}
return 0;
}
```
当输入的字符串为回文数时,输出结果为“该字符串是回文数”;当输入的字符串不为回文数时,输出结果为“该字符串不是回文数”。
阅读全文
相关推荐
















