0
#include <stdio.h>
#include <string.h>

/* Function prototypes */
void wordLength ( char *word );
void wordConcat ( char *wordC1, char *wordC2);
void capitalizeString ( char *sentence);
void birthFormat ( char *birthday, char *bArray);


int main (void)
{

    int choice;
    char word [20];
    char wordC1 [20];
    char wordC2 [20];
    char sentence [200];
    char birthday [10];


    char *ptr1 = "January";
    char *ptr2 = "February";
    char *ptr3 = "March";
    char *ptr4 = "April";
    char *ptr5 = "May";
    char *ptr6 = "June";
    char *ptr7 = "July";
    char *ptr8 = "August";
    char *ptr9 = "September";
    char *ptr10 = "October";
    char *ptr11 = "November";
    char *ptr12 = "December";

    char* bArray[12];

    bArray[0] = ptr1;
    bArray[1] = ptr2;
    bArray[2] = ptr3;
    bArray[3] = ptr4;
    bArray[4] = ptr5;
    bArray[5] = ptr6;
    bArray[6] = ptr7;
    bArray[7] = ptr8;
    bArray[8] = ptr9;
    bArray[9] = ptr10;
    bArray[10] = ptr11;
    bArray[11] = ptr12;







    printf( "Choose a function by enterting the corresponding number: \n"
        "1) Determine the length of a string\n"
        "2) Concatonate 2 different words with a % between them\n"
        "3) Convert a sentence to all capital letters\n"
        "4) Convert birthday from MM/DD/YYYY format to Month, DD, YYYY\n"
        "5) End Program\n");

    scanf( "%d", &choice );
    flushall();

    while (choice >= 1 && choice < 5) 

{

        /* if statements for appropriate user prompt and calls function */
        if (choice == 1) 
        {
            /* gather user input */
        printf( "\nYou have chosen to determine word length.\n"
                "Please enter the word:\t");
            gets( word );

            /* call function to output string as well as the string length */
            wordLength( word );


        }

        else if (choice == 2)
        {
            printf( "\nYou have chosen to concatenate 2 words with a % symbol in between them.\n"
                "Please enter word 1:\t");

            gets( wordC1 );

            printf("Please enter word 2:\t");

            gets( wordC2 );                     

            /* call function to output string as well as the string length */
            wordConcat( wordC1, wordC2 );
    }
        else if (choice == 3)
        {
            printf( "\nYou have chosen to convert a sentence to all capital letters.\n"
                "Please enter the sentence:\t");

            gets( sentence );


            /* call function to output string as well as the string length */
            capitalizeString ( sentence );
    }
        else if (choice == 4)
        {
            printf( "\nYou have chosen to convert your birthday from the format MM/DD/YYYY to Month, DD, YYYY\n"
                "Please enter your birthday:\t");

            gets( birthday );


            /* call function to output string as well as the string length */
            birthFormat ( birthday, bArray );
    }

}
}
void wordLength( char *word )

{
    int length;

    printf( "\nThe string entered is:  %s\n\n", word);

    length = strlen (word);

    printf("The string length is: %d\n", length);


}

void wordConcat(char *wordC1, char *wordC2)

{
    char symbol = "%";
    char result [30];
    printf( "\nThe first word entered is:  %s\n", wordC1);
    printf( "\nThe second word entered is:  %s\n", wordC2);

        strcpy(result, wordC1);
        strcat(result, "%");
        strcat(result, wordC2);

    printf("Output of both strings is: %s\n", result);


}

void capitalizeString(char *sentence)

{

    int i = 0;
    char letter;
    printf("\nThe sentence that you have entered is: %s\n\n", sentence);

    printf("The converted sentence is: \n");
    while (sentence[i])
    {
        letter = sentence[i];
        putchar (toupper(letter));
        i++;
    }
}

void birthFormat(char birthday, char* bArray)
{
printf("\n [%s]\n", bArray[1]);
}

我一直在尝试编写这个程序超过 8 个小时,并且在最后一部分,只是无法弄清楚最后一点!

出于某种原因,每当我将数组从主函数传递到函数birthFormat 时,它都不会通过。数组通过后为空!(我认为)

4

4 回答 4

1

当你传入它时, bArray 不是一个 char* barray (一个字符数组),它是一个 char 指针数组。所以你可以做 char**

于 2012-11-12T03:53:34.940 回答
0

问题(或一个问题)是bArray函数的参数与您传入的变量的类型不同:

char* bArray[12];    // Here's your variable, which can be treated as char ** type.

void birthFormat(char birthday, char** bArray)    // So change the function signature here.
{
    printf("\n [%s]\n", bArray[1]);   // This line will use bArray[1], which is of type char * -- basically the C "string" type.
}

Edit,唯一的变化是通过添加一个*字符来编辑函数的签名。

于 2012-11-12T03:55:58.053 回答
0

将“birthFormat”函数的声明和实现更改为

void birthFormat ( char *birthday, char **bArray);
于 2012-11-12T04:01:23.810 回答
0

这应该工作

void birthFormat(char birthday, char* bArray[12])  // The change is to be made here, make bArray an array of char pointers.
{
    printf("\n [%s]\n", bArray[1]);  
}
于 2012-11-12T04:41:48.643 回答