编写C语言程序,定义一个字符串数组str,字符串数组的值为“fdgs345va9”,使用字符指针的方式遍历这个字符串数组,将其中的小写字母过滤出来(“fdgsva”),并保存到一个新的数组str_new中,打印新的数组str_new。
时间: 2024-02-22 16:58:43 浏览: 36
好的,以下是您要求的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str[MAX_LEN] = "fdgs345va9";
char str_new[MAX_LEN];
int j = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str_new[j++] = str[i];
}
}
str_new[j] = '\0';
printf("原字符串为:%s\n", str);
printf("新字符串为:%s\n", str_new);
return 0;
}
```
程序解释:
1. 定义了一个字符串数组 `str`,并赋值为 `"fdgs345va9"`。
2. 定义了一个新的字符串数组 `str_new`,用于保存过滤出来的小写字母。
3. 定义了一个变量 `j`,用于记录新数组中下一个要插入字符的位置。
4. 使用 `for` 循环遍历字符串数组 `str` 中的每一个字符。
5. 判断当前字符是否为小写字母,如果是,则将其插入到新数组 `str_new` 中。
6. 最后,将新数组的末尾位置设置为字符串结束符 `'\0'`,并打印出原字符串和新字符串。
7. 返回0表示程序正常结束。
相关推荐
![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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)