1

我现在浏览了两天,还没有找到符合我的解决方案的解决方案。我正在尝试编写一个 Pangram 函数。我尝试了很多方法,但都没有成功。我的函数 IsPangram 总是给it is not Pangram我一些建议。

void isPangram()
{
    int i;
    int count = 0;
    char str[26];
    cout << "Enter a string to check if its Pangram or not: ";
    for (i = 0; i < 26; i++) {
        cin >> str[i];

        if (i >= 97 && i <= 122) {

            cout << "It is Pangram" << endl;
            break;
        } else {
            cout << "it is not Pangram" << endl;
            break;
        }

    }
}

int main()
{
    isPangram();
    system("pause");
}
4

4 回答 4

1

您的代码中有 3 个问题 -

(i) 你试图检查每个字符是否是一个 pangram,这是错误的。

(ii) 对于检查,您正在检查索引i而不是读取的字符str[i].

(iii)由于 的值只能在 0 到 26 之间,因此该语句if ( i >= 97 && i <= 122 )将始终评估为。因此,您总是得到的不是 pangram。falsei

尝试这个 -

void isPangram() {
    int i;
    int count = 0;
    char str[27];
    cout << "Enter a string to check if its Pangram or not: ";

    // Read the string
    cin >> str;

    short flagArr[26]; // Array to flag the characters used
    memset(flagArr, 0, sizeof(flagArr));
    bool panGramFlag = true;

    // Loop through the string and mark the characters read
    for (int i = 0; i < 27; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            flagArr[str[i]-'A'] = 1;
        }
    }

    // Loop through flag array and check if any character is not used.
    for (int i = 0; i < 26; i++) {
        if (flagArr[i] == 0) {
           panGramFlag = false;
           cout << "It is not Pangram" << endl;
           break;             
        }
    }   

    if (panGramFlag)
        cout << "it is Pangram" << endl;

}

int main() {

    isPangram();

    system("pause");
}  
于 2015-11-12T07:39:20.533 回答
0
#include<iostream>
using namespace std;

bool chkpangram(string &str)
{
    int m[26] = {0}, i, index;
    for(i = 0; i < str.length(); i++)
    {
        if (str[i] >= 65 && str[i] <= 90)
            index = str[i] - 65;
        else if(str[i] >= 97 && str[i] <= 122)
            index = str[i] - 97;

        m[index] = m[index]++;
    }
    for (int i=0; i<=25; i++)
        if (m[i] == 0)
            return (false);

    return true;
}

int main()
{
    string str = "The quick brown fox jumps over the lazy dog";
    if(str.length() < 26)
        cout<<"Not a Pangram";
    else
    {
        if(chkpangram(str) == true)
            cout<<"Is a Pangram";
        else
            cout<<"Not a Pangram";
    }
    return 0;
}
于 2017-10-24T06:19:17.553 回答
0

//使用散列的最优代码

string str;
int i,h1[26]={0},flag=0;

for(i=0;str[i]!='\0';i++)
{
    h1[str[i]-'a']++;
}
for(i=0;i<26;i++)
{
    if(h1[i]!=1){
        flag=1;
        break;
    }
}
flag==1?cout<<"No"<<endl:cout<<"Yes"<<endl;

} //希望能帮助到你

于 2020-05-04T18:36:10.977 回答
0

这是一个简单的,逻辑思考

#include<iostream.h>
#include<string.h>
#include<conio.h>
void isPangram()
{
    int i;
    char str[26];
    cout << "Enter a string to check if its Pangram or not: ";
    for (i = 0; i < 26; i++) {
    cin >> str[i];

    if ((str[i] >= 97 && str[i] <= 122)||((str[i] >= 65 && str[i] <= 91))
    {

        cout << "It is Pangram" << endl;
        break;
    } else {
        cout << "it is not Pangram" << endl;
        break;
    }

    }
}

int main()
{
    isPangram();
    getch();
    return 0;
}
于 2016-07-08T09:19:18.293 回答