c语言输出字符串
时间: 2023-06-22 22:18:09 浏览: 63
在C语言中,可以使用printf函数输出字符串。例如,要输出字符串"Hello, World!",可以使用以下代码:
```
printf("Hello, World!");
```
输出结果为:
```
Hello, World!
```
相关问题
c语言输出字符串长度
在C语言中,我们可以使用string.h头文件中的strlen()函数来求字符串的长度。下面是一个例子:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str); printf("The length of the string is: %d\n", len);
return 0;
}
```
输出结果为:
```
The length of the string is: 13
```
c语言输出字符串中指定字符的位置
你可以使用C语言中的字符串处理函数`strchr`来查找某个字符在字符串中第一次出现的位置。该函数的函数原型为:
```c
char *strchr(const char *str, int c);
```
其中,`str`是要查找的字符串,`c`是要查找的字符。
如果找到了该字符,则返回该字符在字符串中第一次出现的位置的指针;如果没找到,则返回`NULL`。
下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
char c;
char *p;
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
printf("请输入要查找的字符:");
scanf("%c", &c);
p = strchr(str, c);
if (p != NULL)
{
printf("字符 %c 在字符串中的位置为:%d\n", c, p - str + 1);
}
else
{
printf("未找到字符 %c\n", c);
}
return 0;
}
```
在上面的代码中,我们先使用`fgets`函数从标准输入中读取一个字符串,然后使用`scanf`函数读取要查找的字符。接着,我们使用`strchr`函数查找该字符在字符串中的位置,并计算出该位置相对于字符串首地址的偏移量,最后输出该偏移量加上1的值,即为字符在字符串中的位置。