はじめての C

文字型のアドレスを 3つ保持する配列を使って 文章を出力するプログラムを作成せよ。

  1. /* pastr */
  2. #include
  3. main()
  4. {
  5. char *pastr[3];
  6. pastr[0] = "This is";
  7. pastr[1] = "That is";
  8. pastr[2] = "there are";
  9. printf("%s a pen.\n", *pastr);
  10. printf("%s a mirror.\n", *(pastr+1));
  11. printf("%s two tables in the room.\n", *(pastr+2));
  12. }

putchar を使って 文字列を順次出力するプログラムを作成せよ。

  1. /* outptarry */
  2. #include
  3. main()
  4. {
  5. char str[] = "aburakadabura";
  6. int i = 0;
  7. while (str[i] != '\0') {
  8. putchar(str[i]);
  9. i++;
  10. }
  11. printf("\n");
  12. }

(追記 上のプログラムは 6月12日に書いたコード calcstr に少し似ている)

文字列へのポインタ配列を利用して 3つの文章をランダムに 20行出力するプログラムを作成せよ。

  1. /* randarray */
  2. #include
  3. #include
  4. main()
  5. {
  6. char *str[3];
  7. int seed, i, j;
  8. str[0] = "ダメダメ ( ; _ ; ) !";
  9. str[1] = "トノ、御無体な . .";
  10. str[2] = "office タン 助けて !";
  11. printf("Input seed : ");
  12. scanf("%d", &seed);
  13. srand(seed);
  14. i = 0;
  15. while (i < 20) {
  16. j = rand() % 3;
  17. printf("%s\n", str[j]);
  18. i++;
  19. }
  20. }

(乱数をつくる関数 srand()と rand() はヘッダーファイルの が必要なはず . . . だけど、なくても上のプログラムが動いた ! ← なぜだかワカラナイヨ)

text: 金山典世さん(稚内北星)のページ