#include <stdio.h>
#include <string.h>
/* Function prototypes */
void wordLength ( char *word );
int main (void)
{
int choice;
char word [20];
printf( "Choose a function by enterting the corresponding number: \n"
"1) Determine if words are identical\n"
"2) Count number of words in sentence provided\n"
"3) Enter two strings to be strung together\n"
"4) Quit program\n" );
scanf( "%d", &choice );
flushall();
while (choice >= 1 && choice < 4)
{
/* 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 );
}
}
}
void wordLength( char *word )
{
printf( "The string entered is: %c\n", *word);
}
每当我输入一个单词时,我的程序只输出字符串的第一个字母。为什么这样做?我的字符串长度被声明为 20,所以我知道这不是问题!我似乎无法弄清楚。谢谢!