如何在C中将功能链接到菜单

我的程序是要给出一个句子作为输入,然后从菜单中选择:1)计算句子的单词,2)对句子进行标题修饰,3)退出程序。我已经分别创建了两个功能,但是我无法将每个功能连接到菜单中的相应选项并使之正常工作。

这是我的代码:

#include <stdio.h>
#include<string.h>
#define MAX 100

int Word_count(char *);

int main(int argc,char** argv)
{
    int choice;
    char str[999];
    printf("Enter a sentence: "); gets(str);

    do
    {
        printf("\n\n-----Menu-----\n\n");
        printf("1. How many words does the scentence contain?\n");
        printf("2. Title the scentence.\n");
        printf("3. Exit\n");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1: Word_count(str);
                break;
            case 2: Title_case();
                break;
            case 3: printf("Exiting program!\n");
                exit(0);
                break;
            default: printf("Invalid choice!\n");
                break;
        }

    } while (choice != 3);

}


void Title_case(void)
{
    char str[MAX]={0};  
    int i;

    printf("Enter a string: ");
    scanf("%[^\n]s",str); 

    for(i=0; str[i]!='\0'; i++)
    {
        if(i==0)
        {
            if((str[i]>='a' && str[i]<='z'))
                str[i]=str[i]-32; 
            continue; 
        }
        if(str[i]==' ')
        {
            ++i;

            if(str[i]>='a' && str[i]<='z')
            {
                str[i]=str[i]-32; 
                continue; 
            }
        }
        else
        {
            if(str[i]>='A' && str[i]<='Z')
                str[i]=str[i]+32; 
        }
    }

    printf("Title Cased string is: %s\n",str);

    return ;
}

int Word_count(char *str){
    int i = 0,len,count= 0;
    len = strlen(str);

    if(str[i] >= 'A' && str[i] <= 'z')
    {
       count ++;
    }

    for (i = 1; i<len; i++) 
    {
        if((str[i]==' ' || str[i]=='\t' || str[i]=='\n')&& str[i+1] >= 'A' && str[i+1] <= 'z')
        {
        count++;
        }
    }
printf("there are %d words",Word_count(str));
return count;

}
forestfhb 回答:如何在C中将功能链接到菜单

Word_count函数中,您要再次调用函数本身(肯定是错误的)。我猜你把它放在那里调试。

将其更改为:printf("there are %d words",count);

本文链接:https://www.f2er.com/3163425.html

大家都在问