【发布时间】:2015-12-08 11:16:52
【问题描述】:
我如何使用数据类型“char”找到字符串的长度我尝试了很多次但我没有工作。我也看到了 cplusplus 教程,但无法修复下面的示例
#include <stdio.h>
#include <iostream>
#include<string>
using namespace std;
int main() {
char str1[80];
char str2[80];
cout << "Enter string 1 : ";
cin.getline(str1,80); cout << endl;
cout << "Enter string 2 : ";
cin.getline(str2,80);cout << endl;
cout << "String 1 is :-> " << (unsigned)strlen(str1) << endl;
cout << "String 1 is :-> " << (unsigned)strlen(str2) << endl;
}
【问题讨论】:
-
应该工作得很好,你有什么问题?
-
使用 strlen (cplusplus.com/reference/cstring/strlen)
-
错误的标题,将
#include <string>更改为#include <cstring>。另一个用于std::string类,而不是 c-string 函数。
标签: c++ string string-length