| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 541 人关注过本帖
标题:如何使用strcmp来解决这个问题?
只看楼主 加入收藏
编程新手888
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
如何使用strcmp来解决这个问题?
题目:程序让用户选择语言,如果输入的月份与字符串数组的月份相同(如我输入May,则与char Eng_month内的May相同,则返回(i=4)).  0为拼音,1为英文,程序必须使用一个函数,它调用2个参数,分别是字符串数组和字符串。函数返回相等的那个月份的index(如我输入May,则与char Eng_month内的May相同,则返回(i=4),若没在字符串里发现,则返回-1,要使用strcmp.

PS:我大约只能写这么多了,求助!,谢谢!

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

int language (char month[12][10],char input_month[10])
{
    int n, x, i=0;
    char Chi_month[12][10]={"yiyue","eryue","sanyue","siyue","wuyue","liuyue","qiyue","bayue","jiuyue","shiyue",
        "shiyiyue","shieryue"};
    char Eng_month[12][10]={"January","February","March","April","May","June","July","Augest","September","October","November","December"};

    puts("This program will access a month's index by its name,u can search in two languages: Finnish and English");
    puts("0-Pingyin");
    puts("1-English");
    printf(">> ");
    scanf("%d",&n);
    puts("Enter the month name in the language u selected.");
    printf(">> ");
    gets(input_month);

    switch(n)
    {
    case 0:
        for (i=0;i<10;i++)
        {
            x = strcmp (input_month, Fin_month[i]);

            printf("Your month is month number %d",i+1);
            return i;
        }
        else
        {
            printf("Error:Your input month name is wrong!");
            return -1;
        }
        break;
    case 1:
        if ( x = strcmp (input_month, Eng_month[i]))
        {
            printf("Your month is month number %d",i+1);
            return i;
        }
        else
        {
            printf("Error:Your input month name is wrong!");
            return -1;
        }
        break;
    default:
        printf("Illegal selection! Plz only choose 0 or 1.");
    }
    return 0;
}

int main(void)
{
    int n;
    char input_month[10], month[12][10];

    n = language (month, input_month);
}
搜索更多相关主题的帖子: 字符串 index 英文 用户 
2011-04-10 06:16
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:10 
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

int language (char month[12][10],char input_month[10])
{
    int n, x, i=0;
    char Chi_month[12][10]={"yiyue","eryue","sanyue","siyue","wuyue","liuyue","qiyue","bayue","jiuyue","shiyue",
        "shiyiyue","shieryue"};
    char Eng_month[12][10]={"January","February","March","April","May","June","July","Augest","September","October","November","December"};

    puts("This program will access a month's index by its name,u can search in two languages: Finnish and English");
    puts("0-Pingyin");
    puts("1-English");
    printf(">> ");
    scanf("%d",&n);
    puts("Enter the month name in the language u selected.");
    printf(">> ");
    gets(input_month);

    switch(n)
    {
    case 0:
        for (i=0;i<12;i++)
        {
            x = strcmp (input_month, Chi_month[i]);
            if(x==0)
            {
            printf("Your month is month number %d",i+1);
            return i;
             }
           if(i==11)
            {
            printf("Error:Your input month name is wrong!");
            return -1;
             }
         }
        break;
    case 1:
        for (i=0;i<12;i++)
        {
            x = strcmp (input_month, Eng_month[i]);
            if(x==0)
            {
            printf("Your month is month number %d",i+1);
            return i;
             }
           if(i==11)
            {
            printf("Error:Your input month name is wrong!");
            return -1;
             }
         }
        break;
    default:
        printf("Illegal selection! Plz only choose 0 or 1.");
        break;
    }
    return 0;
}

int main(void)
{
    int n;
    char input_month[10], month[12][10];

    n = language (month, input_month);
    return 1;
}

[ 本帖最后由 qq1023569223 于 2011-4-10 10:57 编辑 ]

   唯实惟新 至诚致志
2011-04-10 08:34
编程新手888
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-10
收藏
得分:0 
回复 2楼 qq1023569223
楼上同学,抱歉,我漏打一句,程序要使用另一个函数,它调用2个参数,分别为字符串数组和整数,整数参数决定语言选择,这个函数初始化选择语言的月份名数组
2011-04-10 14:13
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:10 
程序代码:
#include<stdio.h>
#include<string.h>

int language ()
{
    int n, i = -1;
    char input_month[10];
    char Chi_month[12][10]={"yiyue","eryue","sanyue","siyue","wuyue","liuyue","qiyue","bayue","jiuyue","shiyue","shiyiyue","shieryue"};
    char Eng_month[12][10]={"January","February","March","April","May","June","July","Augest","September","October","November","December"};

    puts("This program will access a month's index by its name,u can search in two languages: Finnish and English");
    puts("0-Pingyin");
    puts("1-English");
    printf(">> ");
    scanf("%d",&n);
    puts("Enter the month name in the language u selected.");
    printf(">> ");
    fflush(stdout);
    scanf("%s", input_month);

    switch(n)
    {
    case 0:
        for (i=0;i<12 && strcmp( input_month, Chi_month[i]) != 0;i++);
        break;
    case 1:
        for (i=0;i<12 && strcmp( input_month, Eng_month[i]) != 0;i++);
        break;
    default:
        printf("Illegal selection! Plz only choose 0 or 1.");
    }
    if (i < 12) {
        printf("Your month is month number %d",i+1);
    } else {
        i = -1;
    }
    return i;
}

int main(void)
{
    int n;
    n = language ();
    return 0;
}


[ 本帖最后由 voidx 于 2011-4-10 19:09 编辑 ]
2011-04-10 19:07
快速回复:如何使用strcmp来解决这个问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054760 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved