C语言,写程序:编写函数:int fun(char *p),功能是判断一个字符串是否是回文字符串(提示:回文字符串是指正读和反读都一样的字符串),要求从主函数中由键盘输入字符串,调用函数fun后,根据函数fun的返回值,主函数输出是否为回文字符串的判断。 【输入形式】 字符串 【输出形式】 是否为回文串
时间: 2024-02-25 07:53:40 浏览: 38
下面是代码实现:
```c
#include <stdio.h>
#include <string.h>
int fun(char *p) {
int len = strlen(p);
for (int i = 0; i < len / 2; i++) {
if (p[i] != p[len - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char str[100];
printf("请输入一个字符串:\n");
scanf("%s", str);
if (fun(str)) {
printf("%s是回文字符串\n", str);
} else {
printf("%s不是回文字符串\n", str);
}
return 0;
}
```
函数 `fun` 的实现很简单,遍历字符串的前一半,判断每个字符是否与对应的后一半相等,如果有一个不相等就返回0,否则返回1。主函数中先从键盘输入一个字符串,再调用 `fun` 判断是否为回文字符串,并输出判断结果。
相关问题
C语言编写一个函数fun(char* s),该函数的功能是把字符串中的内容逆置
你可以使用指针来实现字符串的内容逆置。以下是一个用C语言编写的函数`fun`的示例代码:
```c
#include <stdio.h>
#include <string.h>
void fun(char* s) {
int length = strlen(s);
char* start = s;
char* end = s + length - 1;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
char str[] = "Hello, World!";
printf("原始字符串:%s\n", str);
fun(str);
printf("逆置后的字符串:%s\n", str);
return 0;
}
```
在这个示例中,函数`fun`接受一个字符指针`s`作为参数,代表要逆置的字符串。首先,使用`strlen`函数获取字符串的长度。然后,使用两个指针`start`和`end`分别指向字符串的首尾字符。通过交换`start`和`end`指向的字符,并且让`start`指针向后移动,`end`指针向前移动,直到`start`指针不再小于`end`指针为止,完成字符串内容的逆置。
在`main`函数中,我们定义了一个字符串`str`,并将其作为参数传递给函数`fun`进行逆置。最后,打印逆置后的字符串结果。
运行上述代码,输出结果如下:
```
原始字符串:Hello, World!
逆置后的字符串:!dlroW ,olleH
```
希望能帮助到你!如有任何疑问,请随时提问。
c语言编写程序: 功能:编写函数long fun ( char *p),将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。 例如:若输入字符串"-1234",则函数把它转换为整数值 -1234。
好的,我可以帮你实现这个功能。以下是C语言实现的代码:
```c
#include <stdio.h>
long fun(char *p) {
long num = 0;
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
}
while (*p != '\0') {
num = num * 10 + (*p - '0');
p++;
}
return sign * num;
}
int main() {
char str[] = "-1234";
long num = fun(str);
printf("The converted number is: %ld\n", num);
return 0;
}
```
函数 `fun` 的参数是一个指向字符数组的指针,它会将该数字字符串转换为一个整数并返回。这个函数首先检查是否有符号,如果有,则将符号标记为负数并将指针向后移动一位。然后使用一个循环遍历字符串中的每个字符,将其转换为相应的数字并将其乘以10的适当次幂,最后将所有数字相加得到整数。如果字符串以 `\0` 结尾,则循环结束并返回该整数。
在 `main` 函数中,我们定义了一个数字字符串并将其传递给 `fun` 函数。然后将函数的返回值打印到终端上,以便我们能够查看转换后的整数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)