C语言*******H*sd*sdlkh*******把该字符串中所有的*都去掉。
时间: 2023-12-22 17:05:54 浏览: 39
以下是一个C语言的函数,可以将字符串中的所有*去掉:
```c
#include <stdio.h>
void removeStars(char* str) {
int i, j;
for (i = 0, j = 0; str[i] != '\0'; i++) {
if (str[i] != '*') {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[] = "C语言*******H*sd*sdlkh*******";
removeStars(str);
printf("去掉*后的字符串:%s\n", str); // 输出:C语言Hsdsdlkh
return 0;
}
```
相关问题
c语言字符串*****
C语言中的字符串是由字符组成的字符数组。在C语言中,字符串以空字符('\0')作为结束标志。
以下是一个简单的示例,演示如何定义和操作字符串:
```c
#include <stdio.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
// 使用字符串函数拼接两个字符串
strcat(str1, str2);
printf("拼接后的字符串为:%s\n", str1);
// 使用字符串函数复制一个字符串到另一个字符串
strcpy(str2, str1);
printf("复制后的字符串为:%s\n", str2);
// 使用字符串函数获取字符串的长度
int len = strlen(str1);
printf("字符串的长度为:%d\n", len);
return 0;
}
```
这段代码定义了两个字符数组 `str1` 和 `str2`,分别存储了 "Hello" 和 "World"。使用 `strcat` 函数可以将两个字符串拼接在一起,并且将结果保存在 `str1` 中。使用 `strcpy` 函数可以将一个字符串复制到另一个字符串中。使用 `strlen` 函数可以获取字符串的长度。
注意,在操作字符串时需要确保目标字符数组有足够的空间来存储结果,以防止内存溢出。
c语言在字符串中插入*
在C语言中,可以使用指针和循环来在字符串中插入`*`符号。以下是一个示例代码:
```c
#include <stdio.h>
int main() {
char str[100];
int i, j;
printf("请输入字符串:");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') {
printf("* ");
} else {
printf("%c", str[i]);
}
}
return 0;
}
```
上述代码中,我们使用了`fgets()`函数来读取用户输入的字符串,并使用`for`循环遍历整个字符串。在循环中,如果当前字符是空格,则输出`* `,否则输出该字符本身。
注意,此代码假设用户输入的字符串不超过100个字符,如果需要处理更长的字符串,可以相应地调整数组大小。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)