gpt4 book ai didi

c - 在循环中查找字符串中的某些单词

转载 作者:行者123 更新时间:2023-11-30 18:27:07 25 4
gpt4 key购买 nike

问题是:用户必须输入一个单词和一系列句子。输出必须是唯一包含给定单词的句子(尽管有符号大小写(大/小 - 无关紧要))

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{

char key[25];
char sentence[20][255];
char* sentence_p = (char*)sentence;
char copy[20][255];
int kst, zero = 0, lengs;


printf("Enter your key-word:\n");
gets_s(key, 25);
lengs = strlen(key); //counting how many symbols there are in a given word without zerosymbol
lengs--;


puts("How many sentense you write?");
scanf_s("%d", &kst); //the quantity of the sentences to be written
getchar();

printf("Enter your sentence:\n");

for (int i = 0; i < kst; i++)
gets_s(sentence[i], 225); //sentence input

for (int i = 0; i < kst; i++)
strcpy_s(copy[i], sentence[i]); //copying array in other array

for (int i = 0; i < strlen(key); i++)
key[i] = tolower(key[i]);

for (int i = 0; i < 20 * 255; i++)
*(sentence_p + i) = tolower(*(sentence_p + i)); //all the symbols are in lower case



printf("\n\n\n\n");
char* key_p = key;
int z = 0;
int j,k ;
for (j = 0; j < 255 * kst; j++) {
for (k = 0; k < lengs; k++)
{
if (j != 0 && (*(sentence_p + j - 1) != ' ')) { //element before the word is space
z = 0;
continue;
}

if ((*((key_p)+k) == *((sentence_p + j) + k))) //word=word
z++;
else
z = 0;
}
if (*(sentence_p + j + k + 1) != ' ') {//element after the word is space
z = 0;
continue;
}


if (z == lengs - 1) {
int k = j / 20;
puts(copy[k]);
continue;
}

z = 0;
}


if (zero == 0)
puts("Not found ");
getchar();
return 0;
}

我遇到的问题是:1)输出总是“未找到”2) 或者它输出超出数组的符号

最佳答案

无论你做什么,这段代码总是会输出“未找到”:

int kst, zero = 0, lengs;

// a lot of irrelevant stuff skipped

if (zero == 0)
puts("Not found ");

关于c - 在循环中查找字符串中的某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320687/

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