从用户输入中搜索两个值-第二个值失败
Searching two values from user input - 2nd value fails
在代码中找不到错误。
我正试图在用户输入的句子中找到good
或great
。
在搜索good的时候可以,但是搜索great的时候不行。
#include<iostream>
#include<fstream>
using namespace std;
bool read1();
void write()
{
ofstream fout("sentData.txt",ios::out);
char sentence[1000]={' '};
cout<<"Enter a sentence to check whether it contains "good" or "great" : "<<endl;
cin.getline(sentence,1000);
fout<<sentence;
}
int read()
{
ifstream fin("sentData.txt", ios::in);
char sentence[100] = { ' ' };
char good[5] = { 'g', 'o', 'o', 'd', ' ' };
int count;
bool check=0;
while (!fin.eof())
{
fin.getline(sentence, 150);
for (int i = 0; sentence[i] != ' '; i++)
{
int k = 0;
count = 0;
for (int j = i; sentence[j] != 32; j++)
{
if (sentence[j] == good[k])
{count++;
}
k++;
}
if(count==4)
{
check=1;
break;
}
}
if(count==4)
{
break;
}
}
fin.close();
bool x=read1();
if(check==1 || x==1)
cout<<"Positive sentence."<<endl;
}
bool read1()
{
ifstream in("sentData.txt", ios::in);
char sentence[100] = { ' ' };
char great[6]={'g','r','e','a','t',' '};
int count;
while(!in.eof())
{
in.getline(sentence,100);
for (int i = 0; sentence[i] != ' '; i++)
{
int k = 0;
count=0;
for (int j = i; sentence[j] != 32; j++)
{
if (sentence[j] == great[k])
{count++;
}
k++;
}
if(count==5)
{
return 1;
break;
}
}
}
}
int main()
{
write();
read();
return 0;
}
在字符数组中搜索子字符串可能需要两次循环。一种更快的方法是在句子中搜索关键文本的第一个字母(例如:"好")。如果找到第一个字母,则搜索键的其余部分:
static const char sentence[]="No good nor great deed goes unpunished";
static const char key_text[] = "good";
const unsigned int sentence_length = strlen(sentence);
unsigned int index = 0;
// Outer loop, searching for first character of key text.
bool found = false;
for (unsigned int i = 0;(!found) && (i < sentence_length); ++i)
{
if (sentence[i] == key_text[0])
{
// First character found now, examine remaining key text
// Perform some preliminary checks, such as there must be
// enough letters remaining in the sentence.
const unsigned int key_length = strlen(key_text);
const unsigned int remaining_length = sentence_length - i;
if (remaining_length >= key_length)
{
unsigned int k = 0;
found = true;
for (k = 0; k < key_length; ++k)
{
if (sentence[i + k] != key_text[k])
{
found = false;
break; // Exit the "for" loop.
}
}
}
}
}
if (found)
{
std::cout << "Key was found in sentence.n";
}
else
{
std::cout << "Key not found in sentence.n";
}
一些笔记:
1. 在初始化数组时,不需要单独指定每个字符。
2. 对空间进行比较是没有意义的,因为对空间进行比较会增加复杂性。空格不能等于键中的第一个字符。
3.您可以通过以下方式加快搜索速度:length(sentence) - length(key_text)。
4. 还有更有效的方法,在网上搜索"c++高效子字符串搜索"应该会产生很多信息。
5. 此外,要养成习惯,对不会改变的变量使用const
,比如关键文本。
6. bool
类型使用true
或false
。标识符true
和false
是编译器识别的关键字,因此不需要定义它们。
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++ std::向量插入两个元素替代算法失败
- 即使这两个值低于阈值,BOOST_CHECK_CLOSE也会失败
- 从用户输入中搜索两个值-第二个值失败
- 用替代操作员提升精神 '|' 失败!当有两个可能的规则要走时
- 如果两个值相同,为什么断言失败
- 比较具有相同持续时间的两个std::chrono::time_points失败
- 如何使编译失败,如果两个或没有定义预处理器宏
- 两个glfloat为对的c++ 11向量初始化失败