Yahoo奇摩知識+ 將於 2021 年 5 月 4 日 (美國東部時間) 終止服務。自 2021 年 4 月 20 日 (美國東部時間) 起，Yahoo奇摩知識+ 網站將會轉為唯讀模式。其他 Yahoo奇摩產品與服務或您的 Yahoo奇摩帳號都不會受影響。如需關於 Yahoo奇摩知識+ 停止服務以及下載您個人資料的資訊，請參閱說明網頁。

Yu 發問時間： 電腦與網際網路程式設計 · 9 年前

# C語言 程式問題

1. Write an English word guessing game. (6 points) The computer randomly selects an English word from a list of at least 10 English words. (10 points) The computer scrambles and displays the word. (6 points) The player can guess the word until guessing the correct word. (2 points) They player can decide to continue to play or not.EX.

The word to guess: mgica

=> Try again!

=> Correct!

===========================================

Continue (Y/N)? Y

The word to guess: deia

...

===========================================

Continue (Y/N)? N

Bye!

2 個已更新項目:

### 1 個解答

• 3M
Lv 6
9 年前
最佳解答

#include<stdio.h>

#include<stdlib.h>

char words[][20]={ "apple"

,"cat"

,"dog"

,"egg"

,"fish"

,"girl"

,"magic"

,"moon"

,"elephant"

};

char * scramble_str(char *str)

{

char * out_str = malloc(sizeof(char)*strlen(str));

int i ;

strcpy(out_str,str);

for(i=0;i<strlen(out_str)-1;i++)

{

char tmp ;

int r = i + (rand()%(strlen(out_str)-i)) ;

tmp = out_str[i];

out_str[i] = out_str[r];

out_str[r] = tmp ;

}

return out_str;

}

int main()

{

int num_words = 10 ;

int no_guess ;

char *input_str, *guess_str ;

srand(time(NULL));

while(1)

{

no_guess = rand()%10 ;

printf("The word to guess: %s\n",guess_str=scramble_str(words[no_guess]));

scanf(" %s",input_str);

while( strcmp(words[no_guess],input_str)!=0 )

{

printf("=> Try again!\n");

scanf(" %s",input_str);

}

printf("\n=> Correct!\n");

printf("===========================================\n");

printf("Continue (Y/N)? ");

char ch ;

scanf(" %c",&ch);

if(ch=='N')

break ;

}

printf("\nBye!\n");

system("pause");

return 0 ;

}

2012-01-04 10:51:30 補充：

可以把你的步驟說一次嗎?

因為我測沒問題

2012-01-06 15:35:10 補充：

咦?@@

不如上yahoo即時通討論?

yel155653@yahoo.com.tw