- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
因此,我正在尝试编写一个函数来检查一个单词是否在一个句子中,方法是遍历一个 char 数组并检查相同的 char 字符串。只要 Sentence 没有任何空格,该程序就可以运行。我用谷歌搜索,它们都是相同的建议;
cin.getline
但是无论我如何实现它,它要么不运行,要么跳过整个输入并直接输出。
如何计算空格?
#include <iostream>
using namespace std;
bool isPartOf(char *, char *);
int main()
{
char* Word= new char[40];
char* Sentence= new char[200];
cout << "Please enter a word: ";
cin >> Word;
cout << endl << "Please enter a sentence: ";
//After Word is input, the below input is skipped and a final output is given.
cin.getline(Sentence, 190);
cout << endl;
if (isPartOf(Word, Sentence)==true)
{
cout << endl << "It is part of it.";
}
else
{
cout << endl << "It is not part of it.";
}
}
bool isPartOf(char* a, char* b) //This is the function that does the comparison.
{
int i,j,k;
for(i = 0; b[i] != '\0'; i++)
{
j = 0;
if (a[j] == b[i])
{
k = i;
while (a[j] == b[k])
{
j++;
k++;
return 1;
if (a[j]=='\0')
{
break;
}
}
}
}
return 0;
}
而且我不允许使用 strstr 进行比较。
最佳答案
好的,我会尽力解释你的问题:
假设这是您的输入:
thisisaword
this is a sentence
当您使用 cin 并为其提供任何输入时,它会在换行符处停止,在我的示例中,换行符跟在“thisisaword”中的字符“d”之后。
现在,您的 getline 函数将读取每个字符,直到它停止换行符。
问题是,getline遇到的第一个字符已经是一个换行符,所以它会立即停止。
这是怎么回事?
我会试着这样解释:
如果这是您给程序的输入(注意\n 字符,将其视为单个字符):
thisisaword\n
this is a sentence\n
你的 cin 函数将接受和离开什么:
\n
this is a sentence\n
现在 getline 看到这个输入并被指示获取每个字符,直到它遇到一个换行符,即“\n”
\n <- Uh oh, thats the first character it encounters!
this is a sentence\n
cin 读取输入并留下“\n”,其中 getline 包含“\n”。
要克服这个问题:
\n <- we need to get rid of this so getline can work
this is a sentence\n
如前所述,我们不能再次使用 cin,因为它什么都不做。我们可以使用不带任何参数的 cin.ignore() 并让它从输入中删除第一个字符或使用 2x getline(第一个将获取剩余的\n,第二个将获取带有\n 的句子)
你也可以避免这种问题切换你的cin >> Word;到 getline 函数。
因为它被标记为 C++,所以我将 Char*[] 更改为 Strings 以用于此示例:
string Word, Sentence;
cout << "Please enter a word: "; cin >> Word;
cout << endl << Word;
cin.ignore();
cout << "\nPlease enter a sentence: "; getline(cin, Sentence);
cout << endl << Sentence;
或
string Word, Sentence;
cout << "Please enter a word: "; getline(cin, Word);
cout << endl << Word;
cout << "\nPlease enter a sentence: "; getline(cin, Sentence);
cout << endl << Sentence;
关于c++ - std::cin 跳过空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27205251/
我有这个代码来查找这个模式:201409250200131738007947036000 - 1,在文本内 final String patternStr = "(\\d{
我正在尝试使用正则表达式清除一些用户输入,以删除 [ 和 ] 并删除任何大于 1 个空格的空格。但我似乎无法实现我想要的效果。这是我第一次使用正则表达式,所以我对如何写出来有点困惑。 (preg_re
我正在尝试构建这个简单的正则表达式来匹配 Java 中的单词+空格,但我在尝试解决它时感到困惑。该网站上有很多类似的示例,但答案大多给出了正则表达式本身,而没有解释它是如何构造的。 我正在寻找的是形成
好吧,我已经阅读了很多建议如何消除多余空间的帖子,但无论出于何种原因,我似乎无法将这些建议应用到我的系统中,所以我在这里寻求您的帮助。 这些是我代码的最后几行: for line in rli
所以我正在我的测试存储上学习网页抓取,但我不确定如何正确地从“sizes”数组中删除空的新行。 const $ = cheerio.load(body) $('div.lis
这个问题已经有答案了: How to prevent invalid characters from being typed into input fields (8 个回答) 已关闭 9 年前。 是
有人知道如何让扫描仪忽略空间吗?我想输入名字和第二个名字,但扫描仪不让我输入,我想保存全名 String name; System.out.print("Enter name: "); name =
这个问题在这里已经有了答案: Make Vim show ALL white spaces as a character (23 个回答) 关闭 8 年前。 VIM(使用 Solarized Dar
我想使用 StreamTokenizer 从 java 文件中提取名称。我已将空格设置为逗号 inputTokenizer.whitespaceChars(',', ','); 但是,
我正在使用此代码逐行读取 txt 文件。 // Open the file that is the first command line parameter FileInputStream fstre
我似乎无法弄清楚我需要的正则表达式。这就是我想要实现的目标: {ANY CHAR} + @javax.persistence.Column(name = "{ANY 30 CHARS}") + {AN
我正在运行 StyleCop(顺便说一句,如果你想提供高质量的代码,我完全推荐它)... 我有这条线 [System.Xml.Serialization.XmlRootAttribute(Namesp
我刚刚更新到 PhpStorm 2016,我突然注意到,每次我按 Ctrl + S 保存文件时,它都会删除我在测试这段代码后按下以继续编写的空格/制表符。 请帮忙,这对我来说很烦人,因为我在每一行代码
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我已经看过几十个关于这个主题的问题和答案,但我仍然无法解决我的问题。 我在我的代码中使用了一个外部 ffmpeg 转换器,我将文件路径作为参数传递,如下所示: OutputPackage oo = c
谁能详细解释一下它们是什么以及它们之间的区别。提前致谢。 最佳答案 转义序列是代表其他内容的字符序列。例如(“\n” = 新行,“\?” = 问号等)。有关更详细的列表,请检查:https://en.
我无法从我的 javascript 文本中删除换行符。这是我正在处理的数据示例: 0: "Christian Pulisic" 1: "↵" 2: "From Wikipedia, the free
我有一个问题 - 我似乎无法从字符串的开头/结尾删除新行/空格。我在正则表达式的开头和结尾使用 \s ,甚至在获取字符串后使用 .trim() ,但无济于事。 public void extractI
我是 php 的新手,我正在尝试将一系列变量添加到 html 超链接中。但是,任何返回空格的变量都会弄乱超链接。 Grants Test
我是一名优秀的程序员,十分优秀!