C++中的Pangram函数

wn9m85ua  于 2023-06-07  发布在  其他
关注(0)|答案(4)|浏览(80)

我已经浏览了两天了,还没有找到符合我的解决方案的解决方案。我正在写一个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");
}
yqyhoc1h

yqyhoc1h1#

你的代码中有三个问题-
(i)你试图检查每个字符是否是一个pangram,这是错误的。
(ii)对于检查,检查索引i,而不是读取的字符str[i]
(iii)由于i的值只能在0和26之间,因此该语句if ( i >= 97 && i <= 122 )的计算结果始终为false。因此你总是得不到一个pangram。
试试这个

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");
}
hfwmuf9z

hfwmuf9z2#

#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;
}
r6hnlfcb

r6hnlfcb3#

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

#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;
}
qnakjoqk

qnakjoqk4#

//使用哈希优化代码

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;

} //希望对你有帮助

相关问题