簡體   English   中英

如何掃描用戶的單詞數以打印字符串?

[英]How to scan the number of words from user to print the string?

在我的一個分配問題中,首先我需要掃描用戶的單詞數,然后在給定字符串中找到特定單詞(例如單詞示例)之后。

例:

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM