写一个查找HI!XIDIAN中I的C语言程序
时间: 2023-08-31 15:40:19 浏览: 79
### 回答1:
你可以使用以下C语言程序来查找HI!XIDIAN中的I: ```
#include <stdio.h>
int main()
{
char str[] = "HI!XIDIAN";
int i;
for (i=0; str[i] != '\0'; i++)
{
if (str[i] == 'I')
{
printf("找到 'I' 在位置 %d\n", i);
}
}
return 0;
}
```
### 回答2:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "HI!XIDIAN";
char ch = 'I';
int flag = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ch) {
printf("%c is found at index %d\n", ch, i);
flag = 1;
}
}
if (!flag) {
printf("%c is not found in the string\n", ch);
}
return 0;
}
这段程序可以实现在给定字符串"HI!XIDIAN"中查找字符'I'的功能。首先,我们定义一个字符串数组str,存储待查找的字符串;同时,定义一个字符变量ch,存储要查找的字符。然后,设定一个标志变量flag,初始化为0。
通过使用for循环遍历字符串str,逐个检查字符串中的字符是否与目标字符ch相等。如果发现相等的字符,就打印出该字符及其所在的索引位置,并将标志变量flag设置为1。如果整个字符串都被遍历完毕,但没有找到目标字符,那么标志变量flag的值不会改变,此时输出"目标字符在字符串中未找到"的提示信息。
该程序的运行结果为:"I is found at index 2"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)