Pangram在c ++中的功能

时间:2015-11-12 07:10:02

标签: c++ pangram

我现在正在浏览两天,但找不到符合我解决方案的解决方案。 我正在尝试编写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 个答案:

答案 0 :(得分:1)

您的代码中有3个问题 -

(i)你试图检查每个角色是否是一个庞格拉姆,这是错误的。

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

(iii)此语句if ( i >= 97 && i <= 122 )将始终评估为false,因为i的值只能介于0到26之间。因此,您总是得不到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");
}  

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}

答案 3 :(得分: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;

} //希望有帮助