devc++获取字符串长度代码
时间: 2023-08-16 19:10:01 浏览: 122
可以使用标准库函数 strlen
来获取字符串的长度,示例代码如下:
#include <iostream>
#include <cstring>
int main() {
char str[] = "hello";
int len = strlen(str);
std::cout << "Length of string is: " << len << std::endl;
return 0;
}
该程序输出:Length of string is: 5
,表示字符串 "hello"
的长度为 5。
相关问题
devc++获取字符串变量长度代码
你可以使用 strlen
函数来获取一个字符串变量的长度。以下是一个示例代码:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
cout << "The length of the string is: " << len << endl;
return 0;
}
在此示例中,我们定义了一个字符串变量 str
,然后使用 strlen
函数来获取它的长度,并将结果存储在整型变量 len
中。最后,我们输出了字符串的长度。
devc++如何获取字符串长度
在 Dev-C++ 中,可以使用 strlen
函数来获取一个字符串的长度。strlen
函数位于 string.h
头文件中,可以通过在代码中添加 #include <string.h>
来引入该函数。
以下是一个示例代码,演示了如何使用 strlen
函数来获取字符串的长度:
#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;
}
上述代码定义了一个字符数组 str
,并使用 strlen
函数获取了字符串的长度,最后将长度输出到屏幕上。在 Dev-C++ 中编译并运行该代码,输出结果应为:
The length of the string is: 13
相关推荐
















