如何用c语言程序回答这个问题:定义一函数fun(char a[ ], char c)实现将输入的字符串数组里任意位置的指定字符去除后显示 例如:字符数组a[ ]=“*123*ABC**45678*”,调用该函数fun(a,’*‘);后,数组变为a[ ]=”123ABC45678”
时间: 2023-03-14 15:29:49 浏览: 50
可以使用以下代码实现:void fun(char a[ ], char c)
{
int i, j, k;
for (i = 0; a[i] != '\0'; i++)
{
if (a[i] == c)
{
for (j = i; a[j] != '\0'; j++)
{
a[j] = a[j + 1];
}
}
}
a[i] = '\0';
}
相关问题
使用C语言数组指针实现以下功能:存在函数,原型为:int* fun(char*,float); 1,定义一个函数,该函数的形参为fun函数类型的指针 2,定义一个函数,该函数的返回值为fun函数类型的指针 3,定义fun函数类型的指针数组
好的,以下是使用 C 语言数组指针实现定义一个函数,该函数的形参为 `fun` 函数类型的指针,定义一个函数,该函数的返回值为 `fun` 函数类型的指针,以及定义 `fun` 函数类型的指针数组的代码:
```c
#include <stdio.h>
int* fun(char* str, float f) {
static int result[2];
result[0] = strlen(str);
result[1] = (int)f;
return result;
}
void test(int* (*p)(char*, float)) {
char str[100];
float f;
printf("请输入一个字符串和一个浮点数:\n");
scanf("%s %f", str, &f);
int* result = p(str, f);
printf("字符串长度:%d\n", *result);
printf("浮点数转换为整数:%d\n", *(result+1));
}
int* (*fun_ptr)(char*, float);
int main() {
fun_ptr = fun;
test(fun_ptr);
int* (*fun_ptr_array[5])(char*, float);
fun_ptr_array[0] = fun;
printf("fun_ptr_array[0](\"hello\", 3.14) = {%d, %d}\n", *(fun_ptr_array[0]("hello", 3.14)), *(fun_ptr_array[0]("hello", 3.14)+1));
return 0;
}
```
这个程序中,我们首先定义了一个函数 `fun`,该函数的参数为一个字符串和一个浮点数,返回值为一个包含两个整数的数组指针。在函数中,我们使用 `strlen` 函数计算字符串长度,并将浮点数转换为整数,将结果存储在静态数组中,并返回该数组指针。
接着,我们定义了一个名为 `test` 的函数,该函数的参数为一个函数指针,指向一个 `fun` 类型的函数。在函数中,我们提示用户输入一个字符串和一个浮点数,调用传入的函数指针 `p`,并将结果存储在 `result` 变量中。最后,我们输出字符串长度和浮点数转换为整数的结果。
在 `main` 函数中,我们定义了一个 `fun` 函数类型的指针 `fun_ptr`,并将其指向 `fun` 函数。然后,我们调用 `test` 函数,传入 `fun_ptr` 作为参数。接着,我们定义了一个长度为 5 的 `fun` 函数类型的指针数组 `fun_ptr_array`,并将其第一个元素指向 `fun` 函数。最后,我们输出 `fun_ptr_array[0]` 函数的结果。
需要注意的是,在实际应用中,可以将 `fun` 函数类型的指针定义为一个更具有实际意义的类型,例如函数指针类型 `typedef int* (*FunPtr)(char*, float)`,这样可以提高代码的可读性。
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
```
希望能帮助到你!如有任何疑问,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)