c++ - 如何检查二维char数组是否有空字
问题描述
这里有两个保存某些单词的二维字符数组。我正在尝试创建一个函数,如果双数组包含空 C 字符串 ('') 的单词,则返回 false,如果它的每个单词包含至少一个字母,则返回 true。
#include <iostream>
using namespace std;
const int MAX_WORD_LENGTH = 20;
bool checkEmptyString(const char word[][MAX_WORD_LENGTH + 1], int numOfWords);
int main()
{
const char dict1[][MAX_WORD_LENGTH + 1] = {"Hello", "What", ""};
const char dict2[][MAX_WORD_LENGTH + 1] = {"Hello", "Hey" "Man", "Sup"};
if (checkEmptyString(dict1, 3))
cout << "Dictionary 1 is empty!" << endl;
else
cout << "Dictionary 1 is not empty!" << endl;
if (checkEmptyString(dict2, 4))
cout << "Dictionary 2 is empty!" << endl;
else
cout << "Dictionary 2 is not empty!" << endl;
system("pause");
return 0;
}
bool checkEmptyString(const char word[][MAX_WORD_LENGTH + 1], int numOfWords)
{
// Enter code here ... //
}
如何以生成所需结果的方式实现该功能?
解决方案
for(int i=0; i<numOfWords; i++) {
int j=0;
while(word[i][j]) j++;
if(!j) return true;
}
return false;
我认为这将完成这项工作..
推荐阅读
- delphi - TMemo 中的空格与文本文件不匹配
- swift - (UIApplication.shared.keyWindow?.rootViewController as?BaseSlidingController)?.openMenu() return nil Swift 4.2
- jquery - bootstrap 4 轮播控制位置
- python - 基于最频繁值的子集
- java-3d - 多个 Canvas3D 的 OrbitBehavior
- amazon-web-services - Packer Amazon-EBS 构建器示例构建失败:等待 SSH 超时
- c# - FileStream 类 C# 从 txt 文件输入到数组
- json - 在联接中使用时,Oracle JSON 列导致标识符无效
- java - java - 为多个值创建不同的变量与 1 个变量?
- android - 使用 FlowTextView 包装文本的 Android 正在覆盖 TextView