C查询一个char数组中的“’”的位置,并将其打印出来
时间: 2024-03-05 13:52:32 浏览: 12
可以使用循环遍历`char`数组,找到第一个出现的`'`字符,然后输出其位置。代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World! It's a beautiful day.";
int len = strlen(str);
int pos = -1;
for (int i = 0; i < len; i++) {
if (str[i] == '\'') {
pos = i;
break;
}
}
if (pos != -1) {
printf("The position of ' is: %d\n", pos);
} else {
printf("The character ' is not found.\n");
}
return 0;
}
```
输出:
```
The position of ' is: 13
```
相关问题
C查询一个char数组中所有“’”的位置,并将其打印出来
可以使用循环遍历`char`数组,找到每个出现的`'`字符,然后输出其位置。代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World! It's a beautiful day. Don't you think so?";
int len = strlen(str);
printf("The positions of ' are:");
for (int i = 0; i < len; i++) {
if (str[i] == '\'') {
printf(" %d", i);
}
}
printf("\n");
return 0;
}
```
输出:
```
The positions of ' are: 13 28 43
```
C语言生成char类型数组并打印
以下是一个示例程序,演示如何生成一个char类型数组并打印出来:
```c
#include <stdio.h>
int main() {
char str[] = "Hello, World!"; // 生成一个char类型数组
printf("%s", str); // 打印数组
return 0;
}
```
输出结果为:
```
Hello, World!
```
在这个程序中,我们首先声明了一个char类型数组`str`,并将其初始化为`"Hello, World!"`。然后我们使用`printf`函数打印数组,用%s作为占位符,表示我们要打印一个字符串,然后将数组名`str`作为参数传递给`printf`函数。
注意,由于`str`是一个字符数组,它的末尾会自动添加一个空字符`\0`,因此`printf`函数会一直打印数组元素,直到遇到`\0`为止。