山东科技大学OJ2694 查找单词

Description

          给出n个英文单词,查找出其中正数或倒数第p个字符是c有哪些。

Input

          输入分为两部分。

         第一部分首先是一个整数n(n<=500),后接n个长度小于30的英文单词,这些单词均为小写字母组成。

         第二部分是不超过100行,每行是一次查找,至EOF结束。每次查找的输入为一个英文字母c和一个整数p,用一个分隔开。若p为正,则在n个单词中查找左起第p个字符为c的所有单词;若p为负,则在n个单词中查找右起第p个字符为c的所有单词。

Output

          输出为每次查找单词的结果,与输入的第二部分对应。每次查找的结果输出一行,若有多个单词符合查找条件,按照输入的顺序依次输出,两两单词间用一个空格分隔。若没有找到符合条件的单词,那么输出一个空行。

Sample Input

15 to too two teem tool taste tooth be bee bed box bend book below bench t 1 e 2 o 3 h -1 e -2 b -3

Sample Output

to too two teem tool taste tooth
teem be bee bed bend below bench
too two tool tooth book
tooth bench
teem bee bed
bee bed box

题解:

#include<stdio.h>
#include<string.h>
void find(char ch,int d,int n,char list[][31]){
    int temp=0;
    if (d>0){
        d--;
        for (int i = 0; i < n; i++){
            if (d>strlen(list[i])){
                continue;
            }
            if (list[i][d]==ch){
                
                if(temp==0){
                    printf("%s",list[i]);
                }else{
                    printf(" %s",list[i]);
                }
                temp++;
            }
        }
    }else{
        d=-d;
        for (int i = 0; i < n; i++){
            if (d>strlen(list[i])){
                    continue;
            }
            if (list[i][strlen(list[i])-d]==ch){
                if(temp==0){
                    printf("%s",list[i]);
                }else{
                    printf(" %s",list[i]);
                }
            temp++;
            }
        }
    }
    printf("\n");
}
int main()
{
    char list[501][31];
    int n;
    scanf("%d",&n);
    getchar();
    for (int i = 0; i < n; i++){
        gets(list[i]);
    }
    char c;
    int d;
    while (scanf("%c%d",&c,&d)!=EOF){
        getchar();
        find(c,d,n,list);
    }
    return 0;
}

注意:

        1.注意下标不要越界;

        2.注意第p个字母,当p为正数时,要减1才为其下标,p为负数时,字符串长度减1为其下标。

        3.注意两个scanf连用时,当第二个scanf读入的第一个字符为字符类型时,会读入上一次读入时留下的换行符,要将其用getchar吃掉。

        4.scanf与gets连用时同上。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值