gpt4 book ai didi

c - 如何扫描用户的字数并打印字符串?

转载 作者:行者123 更新时间:2023-11-30 16:09:40 24 4
gpt4 key购买 nike

在我的一个作业问题中,首先我需要扫描用户的单词数量,然后需要在给定字符串中找到特定单词(示例中的单词示例)。

示例:

Input

5 // Where 5 indicates the number of word
This is a sample input //need to find the word sample

Output:
4

最佳答案

使用scanf读取单词数,然后一一读取并比较。

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define WORD_MAX_LEN 20

#define SEARCH_VAL "sample"


int main()
{
int num_of_words, i=0;
char word[WORD_MAX_LEN];


//read input words
scanf("%d", &num_of_words);

// search
while (i++ < num_of_words)
{
scanf("%s", word);
if (strcmp(word, SEARCH_VAL) == 0)
{
printf("%d", i);
}
}
}

关于c - 如何扫描用户的字数并打印字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59081854/

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