- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个无法解决的问题,我需要检查第一句的最后三个单词和第四句的最后三个单词
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char firstRow[256];
char secondRow[256];
char thirdRow[256];
char fourthRow[256];
printf("Enter four row of lyrcis:\n");
gets(firstRow);
gets(secondRow);
gets(thirdRow);
gets(fourthRow);
if ( strcmp(a1+strlen(a1)-1, a4+strlen(a4)-1) &&
strcmp(a1+strlen(a1)-2, a4+strlen(a4)-2) &&
strcmp(a1+strlen(a1)-3, a4+strlen(a4)-3) == 0 ){
printf("Good job last three words of first and fourth sentence are same");
}
else {
printf("nothing");
}
return 0;
}
这是我尝试过的东西,但显然问题是我无法使用 if like that with only one strcmp it work。也许我需要 strcpy 命令?救命!
最佳答案
首先 - 不要使用“获取”。这是非常不安全的。它将读取的字符数或您提供的缓冲区大小是否有足够的存储空间没有限制。这允许缓冲区溢出漏洞利用,也是它从 C 库中删除的主要原因。如果您的教授坚持使用它 - 找一位新教授。
您遇到的另一个问题是无法验证流程中的每个步骤。在将指针传递给 strcmp
或 strlen
之前,您无法检查 gets
是否实际读取了任何内容。
此外,你的索引是无稽之谈。 strlen(x) - n
不会索引缓冲区中的 end - n
单词。为此,您必须对字符串进行标记(将其拆分为单词)。有多种方法可以做到这一点。
不管怎样,一种有效的方法就是简单地找到字符串的末尾(例如 strlen(line) - 1
),并使用指针从字符串的末尾向开头迭代,直到找到您的第一个空白(或者到达开头)。
C 库(在 string.h
中)提供了 strrchr
,它可以为您自动执行该过程。它将从字符串末尾开始向后迭代,直到找到您告诉它查找的字符的第一次出现,返回指向该字符的指针(如果未找到该字符,则返回 NULL ) 。这里唯一的缺点是您只能搜索单个字符。
C 库(在 string.h
中)提供了 strtok
,它不提供反向搜索,但确实提供了基于您提供的一组分隔符(例如,它可以处理空格
、制表符
、'.'等中任何一个的分割)。在这里,您只需存储指向每个单词(或单词的副本)的指针,并获取最后 3 个索引进行比较。
下面提供了一个示例,该示例使用 strrchr
假定您的单词或由一个(或多个)空格
分隔。下面使用的方法和 strtok
修改 原始字符串,因此如果字符串最初存储在只读存储器中(例如 < em>字符串文字)。
程序期望读取的文件名作为第一个参数提供(或者如果未提供参数,则将从 stdin
读取)。下面的代码中提供了其他注释:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef BUF_SIZ /* if you need constants... define them */
#define BUF_SIZ 8192 /* don't put 'magic' numbers in code */
#endif
#define NLAST 3
int main (int argc, char **argv) {
size_t len = 0;
char line1[BUF_SIZ] = "",
line4[BUF_SIZ] = "",
*last[NLAST] = { NULL };
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
if (fgets (line1, BUF_SIZ, fp) == NULL) { /* read 1st line */
fprintf (stderr, "error: failed to read line1.\n");
return 1;
}
for (int i = 0; i < NLAST; i++) /* read/discard lines 2,3 read line 4 */
if (fgets (line4, BUF_SIZ, fp) == NULL) {
fprintf (stderr, "error: failed to read line4.\n");
return 1;
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
len = strlen (line1); /* get length of line1 */
if (len && line1[len-1] == '\n') /* validate last is '\n' */
line1[--len] = 0; /* overwrite with nul-character */
else { /* error: handle line too long or no POSIX EOL */
fprintf (stderr, "error: line1 too long or no POSIX EOL.\n");
return 1;
}
len = strlen (line4); /* same thing for line4 */
if (len && line4[len-1] == '\n')
line4[--len] = 0;
else {
fprintf (stderr, "error: line4 too long or no POSIX EOL.\n");
return 1;
}
if (!*line1 || !*line4) { /* test if either is empty-string */
fprintf (stderr, "error: one or both line(s) empty.\n");
return 1;
}
for (int i = 0; i < NLAST; i++) { /* loop NLAST times */
char *p1 = strrchr (line1, ' '), /* get pointer to last ' ' */
*p4 = strrchr (line4, ' ');
if (!p1) { /* validate result of strrchr */
if (i < NLAST - 1) { /* if not last iteration - handle error */
fprintf (stderr, "error: only '%d' words in line1.\n", i+1);
return 1;
}
else /* if last iteration, assign line to pointer */
p1 = line1;
}
if (!p4) { /* same for line4 */
if (i < NLAST - 1) {
fprintf (stderr, "error: only '%d' words in line4.\n", i+1);
return 1;
}
else
p4 = line1;
}
/* copy to last array in order - checking if p1 is beginning of line */
last[NLAST - 1 - i] = p1 == line1 ? p1 : p1 + 1;
while (p1 > line1 && *p1 == ' ') /* nul-terminate at space */
*p1-- = 0;
while (p4 > line4 && *p4 == ' ')
*p4-- = 0;
}
printf ("\nthe last %d words in lines 1 & 4 are the same:\n", NLAST);
for (int i = 0; i < NLAST; i++)
printf (" %s\n", last[i]);
return 0;
}
输入文件示例
$ cat dat/last14-3.txt
My dog has fleas
My snake has none
The cats have none
Cats are lucky because the dog has fleas
示例使用/输出
$ ./bin/lines1_4_last3 < dat/last14-3.txt
the last 3 words in lines 1 & 4 are the same:
dog
has
fleas
无论您选择哪种方法来标记线条,您都必须验证整个过程中的每个步骤。检查一遍并确保您了解为什么每次验证都是必要的,如果不需要,请询问,我很乐意为您提供进一步的帮助。
关于对检查和比较句子中最后三个单词的任务感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47252918/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!