功能:用指针将一个字符串的指定字符替换为另一个字符。 -------------------------------------------------*/ #include<stdio.h> #include<string.h> int main() { char s[80],c1,c2; char *p=s; printf("\n请输入一个字符串:"); gets(s); printf("\n请输入指定替换字符:"); scanf("%c,%c",&c1,&c2); /**************FILL***************/ while(____________!='\0') { if(*p==c1) /**************FILL***************/ _________________; p++; } puts("\n result(结果):"); puts(s); return 0; }
时间: 2023-07-05 18:24:14 浏览: 78
答案如下:
```c
#include<stdio.h>
#include<string.h>
int main() {
char s[80],c1,c2;
char *p=s;
printf("\n请输入一个字符串:");
gets(s);
printf("\n请输入指定替换字符:");
scanf("%c,%c",&c1,&c2);
while(*p!='\0') {
if(*p==c1)
*p=c2;
p++;
}
puts("\n result(结果):");
puts(s);
return 0;
}
```
这个程序通过指针遍历字符串,当遍历到字符c1时,将其替换为c2。最后输出替换后的结果。
相关问题
c语言中一个字符串为Content-Length: 53,怎么提取出53
可以使用字符串处理函数来提取出53。具体实现如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Content-Length: 53";
char *ptr = strstr(str, ":"); // 找到冒号的位置
int len = atoi(ptr + 2); // 将数字串转换为整数
printf("%d\n", len); // 输出53
return 0;
}
```
首先使用 `strstr` 函数找到字符串中冒号的位置,然后使用 `atoi` 函数将冒号后面的数字串转换为整数。注意,因为数字串前面还有一个空格,所以需要将指针偏移2个位置才能得到正确的数字串。
c语言编写一个函数 输入-1234为字符串 输出-1234为整数
以下是一个示例代码:
```c
#include <stdio.h>
int strToInt(char* str) {
int result = 0;
int sign = 1;
if (*str == '-') {
sign = -1;
str++;
}
while (*str != '\0') {
result = result * 10 + (*str - '0');
str++;
}
return sign * result;
}
int main() {
char str[] = "-1234";
int num = strToInt(str);
printf("%d\n", num);
return 0;
}
```
解释一下:
该函数接受一个指向字符串的指针,并返回该字符串表示的整数。首先,我们将符号保存在一个变量中(如果有的话),然后遍历字符串中的每个字符,将其转换为数字并将其乘以10的幂加到结果中。最后,我们将结果乘以符号并返回它。
在主函数中,我们声明一个字符串并将其传递给函数。然后我们将结果打印出来。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)