我现在浏览了两天,还没有找到符合我的解决方案的解决方案。我正在试着写一个Pangram函数。我试过很多方法,但都不能成功。我的函数IsPangram总是给我一些建议的方法。
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");
}
发布于 2015-11-12 15:39:21
你的代码中有3个问题-
(i)您正在尝试检查每个字符是否为pangram,这是错误的。
(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");
}
发布于 2016-07-08 17:19:18
这是一个简单的问题,从逻辑上思考
#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;
}
发布于 2017-10-24 14:19:18
#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;
}
https://stackoverflow.com/questions/33666002
复制相似问题