C++ 我的程序在一个句子中查找最长的单词有什么问题? #包括 使用名称空间std; int main(){ 字符a[101]{0}; cin>>a; cin.getline(a,101); cin.ignore(); int currLen{0}; int maxLen{0}; int startInd{-1}; int endInd{-1}; 对于(int i=0;imaxLen){ maxLen=currLen; startInd=i-curren; endInd=i-1; } 如果(a[i]='\0') 打破 currLen=0; } } 库特
考虑a[i]==0的情况。以下哪项if声明适用 答:第一个。这意味着你永远不会看到字符串中的最后一个单词。您也不会在字符串末尾退出,而是通过字符串中的任何内容循环到101字符 作为一般结构,要非常非常小心:C++ 我的程序在一个句子中查找最长的单词有什么问题? #包括 使用名称空间std; int main(){ 字符a[101]{0}; cin>>a; cin.getline(a,101); cin.ignore(); int currLen{0}; int maxLen{0}; int startInd{-1}; int endInd{-1}; 对于(int i=0;imaxLen){ maxLen=currLen; startInd=i-curren; endInd=i-1; } 如果(a[i]='\0') 打破 currLen=0; } } 库特,c++,algorithm,C++,Algorithm,考虑a[i]==0的情况。以下哪项if声明适用 答:第一个。这意味着你永远不会看到字符串中的最后一个单词。您也不会在字符串末尾退出,而是通过字符串中的任何内容循环到101字符 作为一般结构,要非常非常小心: if (condition) else if (condition) // without a final else section 如果你这样做,你需要考虑你在做什么。在这种情况下,您可以: if (a[i] != 0 && a[i] != ' ') else 它可能不
if (condition)
else if (condition)
// without a final else section
如果你这样做,你需要考虑你在做什么。在这种情况下,您可以:
if (a[i] != 0 && a[i] != ' ')
else
它可能不能解决你所有的问题,但应该能解决一些问题。这里有几件事: 1-您不需要执行
cin>>a
这实际上是在使用第一个单词,然后内容被cin.getline()覆盖。
因此删除第一个cin>>a
就可以了
2-不读取最后一个字,因为没有任何if条件与条件aka匹配
if(a[i]!=' ' ) case of not a space
//not end of word
else if(a[i]==' '||a[i]=='\0') case of space or null
//end of word
因此,您的最后一个字符既不是空格也不是null,这意味着您没有检测到最后一个单词。一个漂亮的滑动窗口模式实现 您的代码中有3个问题
cin>>a代码>
cin.ignore()代码>
if
语句,如下所示:if(a[i]!=''&&a[i]!='\0')
否则您将无法检测到最后一个单词int main()
{
char a[101]{ 0 };
//cin >> a;
cin.getline(a, 101);
//cin.ignore();
int currLen{ 0 };
int maxLen{ 0 };
int startInd{ -1 };
int endInd{ -1 };
for (int i = 0; i < 101; i++)
{
if (a[i] != ' ' && a[i] != '\0')// Add comparison
{
++currLen;
}
else if (a[i] == ' ' || a[i] == '\0')
{
if (currLen > maxLen)
{
maxLen = currLen;
startInd = i - currLen;
endInd = i - 1;
}
if (a[i] == '\0')
break;
currLen = 0;
}
}
cout << maxLen << endl;
if (startInd == -1)
cout << -1;
else
for (int i = startInd; i <= endInd; i++)
cout << a[i];
return 0;
}
intmain()
{
字符a[101]{0};
//cin>>a;
cin.getline(a,101);
//cin.ignore();
int currLen{0};
int maxLen{0};
int startInd{-1};
int endInd{-1};
对于(int i=0;i<101;i++)
{
if(a[i]!=''&&a[i]!='\0')//添加比较
{
++柯伦;
}
else如果(a[i]=''| a[i]='\0')
{
如果(currLen>maxLen)
{
maxLen=currLen;
startInd=i-curren;
endInd=i-1;
}
如果(a[i]='\0')
打破
currLen=0;
}
}
您不使用标准库中的std::string
或任何东西有什么原因吗?请输入一个小错误,然后开始调试。您自己的问题中有最好的线索:“如果我在后面写…带空格”谢谢Joseph,谢谢您这么好的解释。还请告诉我何时使用cin.ignore()函数。
int main()
{
char a[101]{ 0 };
//cin >> a;
cin.getline(a, 101);
//cin.ignore();
int currLen{ 0 };
int maxLen{ 0 };
int startInd{ -1 };
int endInd{ -1 };
for (int i = 0; i < 101; i++)
{
if (a[i] != ' ' && a[i] != '\0')// Add comparison
{
++currLen;
}
else if (a[i] == ' ' || a[i] == '\0')
{
if (currLen > maxLen)
{
maxLen = currLen;
startInd = i - currLen;
endInd = i - 1;
}
if (a[i] == '\0')
break;
currLen = 0;
}
}
cout << maxLen << endl;
if (startInd == -1)
cout << -1;
else
for (int i = startInd; i <= endInd; i++)
cout << a[i];
return 0;
}