首页 > 解决方案 > 如何检查二维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 ... //
}

如何以生成所需结果的方式实现该功能?

标签: c++arraysfunction

解决方案


for(int i=0; i<numOfWords; i++) {
    int j=0;
    while(word[i][j]) j++;
    if(!j) return true;
}
return false;

我认为这将完成这项工作..


推荐阅读