gpt4 book ai didi

对检查和比较句子中最后三个单词的任务感到困惑

转载 作者:行者123 更新时间:2023-11-30 19:32:17 24 4
gpt4 key购买 nike

我有一个无法解决的问题,我需要检查第一句的最后三个单词和第四句的最后三个单词

#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 库中删除的主要原因。如果您的教授坚持使用它 - 找一位新教授。

您遇到的另一个问题是无法验证流程中的每个步骤。在将指针传递给 strcmpstrlen 之前,您无法检查 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com