【发布时间】:2020-12-27 21:53:43
【问题描述】:
#include <iostream>
using namespace std;
int main(){
char word[100];
char count;
int j=0;
cout<<"Enter a word or a phrase"<<endl;
cin>>word;
cout<<endl<<word<<endl;
j=sizeof(word);
cout<<j;
}
我想要在上面的程序中做的是找出用户输入的字符串(单词)的长度,但是上面的程序只是给出了整个数组的大小,即 100。
【问题讨论】:
-
为什么不用
std::string?然后你可以使用std::string::size()。 -
使用数组是任务的一部分。
-
好的。无论如何,考虑一下
using namespace std;is considered a bad practice。 -
@SaimBaloch
assignment所以我假设你在某个学校?如果这是真的,请考虑阅读The top answer to this question。您特别注意“询问家庭作业”/“了解学校政策”。 -
作为提示:一种方法是,如果您只是遍历单词数组,例如 for 循环,检查
word[i]是否为== '\0'然后中断,@987654330 的值@ 将是你想要的。
标签: c++ arrays variables output size