Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查字符串以匹配Char变量_C++_String - Fatal编程技术网

C++ 检查字符串以匹配Char变量

C++ 检查字符串以匹配Char变量,c++,string,C++,String,我已经编写了一个简短的程序,它接受用户输入,然后检查字符串是否与用户输入匹配,但我需要添加另一个函数,该函数检查用户输入是否在字符串中,以及是否返回错误 以下是我的代码供参考: const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ "; int main() { char letter; //Variable holding user entered letter cout << "Please enter l

我已经编写了一个简短的程序,它接受用户输入,然后检查字符串是否与用户输入匹配,但我需要添加另一个函数,该函数检查用户输入是否在字符串中,以及是否返回错误

以下是我的代码供参考:

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
int main()
{
    char letter; //Variable holding user entered letter 
    cout << "Please enter letter in the aplhabet:" << endl;
    cin >> letter;
    cout << "The Position of " << letter << " in the string is: " << ALPHABET.find(letter) << endl;

    return 0;
}
const string ALPHABET=“abcdefghijklmnopqrstuvxyz.,”;
int main()
{
char letter;//保存用户输入字母的变量
信纸;

cout如果您想更有趣,您可以编写自己的函数。但是,
string::find()
是可以的。您只需要检查返回的索引是否有效

// Example program
#include <iostream>
#include <string>

using namespace std;


const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
int main()
{
    char letter; //Variable holding user entered letter 
    string::size_type index; //Index where char is found in string
    cout << "Please enter letter in the aplhabet:" << endl;
    cin >> letter;

    index =  ALPHABET.find(letter);

    if (index == string::npos)
        cout << "Error, letter not found" << endl;
    else
        cout << "The Position of " << letter << " in the string is: " << index << endl;

    return 0;
}
//示例程序
#包括
#包括
使用名称空间std;
常量字符串字母表=“ABCDEFGHIJKLMNOPQRSTUVWXYZ.,”;
int main()
{
char letter;//保存用户输入字母的变量
string::size_type index;//在字符串中找到字符的索引
信纸;
索引=字母表。查找(字母);
if(index==string::npos)

cout如果您想更有趣,您可以编写自己的函数。但是,
string::find()
是可以的。您只需要检查返回的索引是否有效

// Example program
#include <iostream>
#include <string>

using namespace std;


const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,’ ";
int main()
{
    char letter; //Variable holding user entered letter 
    string::size_type index; //Index where char is found in string
    cout << "Please enter letter in the aplhabet:" << endl;
    cin >> letter;

    index =  ALPHABET.find(letter);

    if (index == string::npos)
        cout << "Error, letter not found" << endl;
    else
        cout << "The Position of " << letter << " in the string is: " << index << endl;

    return 0;
}
//示例程序
#包括
#包括
使用名称空间std;
常量字符串字母表=“ABCDEFGHIJKLMNOPQRSTUVWXYZ.,”;
int main()
{
char letter;//保存用户输入字母的变量
string::size_type index;//在字符串中找到字符的索引
信纸;
索引=字母表。查找(字母);
if(index==string::npos)

coutif/else语句听起来不错。如果不起作用,还有其他多种方法可以使用。

if/else语句听起来不错。如果不起作用,还有其他多种方法可以使用。

根据定义,用户输入的任何内容都将是字符串。您希望对co提出什么具体要求字符串的内容?在打印出索引之前,请检查从字母表
获得的返回值。查找
std::string::npos
。这意味着在字符串中找不到用户的条目。如果您已经知道如何做,为什么要问一个问题而不是自己直接尝试?这会很快r、 @omnipotential基本上,用户在字母表中输入一个字符,然后函数将其与字符串进行检查,以找到字符串中单个字符的位置,并将该位置返回给用户。如果用户试图输入字符串“字母表”中未包含的字符,我现在需要它给出并出错@johnaples:那么,实际的问题是什么呢?你有用户的字符,你有搜索它的字符串,还有
find()的结果
,它告诉您是否找到了该字符。如果返回的索引是
std::string::npos
,则输出错误消息,否则输出索引值。这有什么困难?根据定义,用户输入的任何内容都将是一个字符串。您希望对该字符串的内容施加什么具体要求?请说明在打印出索引之前,请检查从字母表中得到的返回值。查找
std::string::npos
。这意味着在字符串中找不到用户的条目。如果您已经知道如何执行此操作,为什么要问问题而不是直接自己尝试?这样会更快。@omnipotenty Bas首先,用户在字母表中输入一个字符,然后函数根据字符串检查该字符,以查找字符串中的单个字符位置,并将该位置返回给用户。如果用户试图输入字符串“字母表”中未包含的字符,我现在需要它给出并出错@johnaples:那么,实际的问题是什么呢?你有用户的字符,你有搜索它的字符串,还有
find()的结果
,它告诉您是否找到了字符。如果返回的索引是
std::string::npos
,则输出错误消息,否则输出索引值。这有什么困难?
std::string::find()
返回
std::string::npos
(-1)如果找不到字符。您需要专门查找该值,而不是检查
index>length()
(这是完全错误的),例如:
std::string::size\u type index;…if(index==std::string::npos)…
std::string::npos只是大小为t的元素的最大可能值。我的解决方案基本上也是如此。不过,感谢您自命不凡。“最大可能值”-您假设
std::string::size_type
始终是无符号的,这并不总是正确的。默认情况下,它是
std::size_t
,确实是无符号的。但是
std::string::size_type
的实际类型取决于使用的
分配器。最好不要对类型或值进行任何假设。
std::string::npos
特别表示“字符串中没有有效位置”,因此这是您应该检查的。否则,
npos
一开始就没有存在的意义。
std::string::find()
返回
std::string::npos
(-1)如果找不到字符。您需要专门查找该值,而不是检查
index>length()
(这是完全错误的),例如:
std::string::size\u type index;…if(index==std::string::npos)…
std::string::npos只是大小为t的元素的最大可能值。我的解决方案基本上也是如此。不过,感谢您自命不凡。“最大可能值”-您假设
std::string::size\u type
始终是无符号的,这并不总是真的。默认情况下,它是
std::size\u t
,确实是无符号的。但是
std::string::size\u type
的实际类型取决于
分配器