はじめての C
文字型のアドレスを 3つ保持する配列を使って 文章を出力するプログラムを作成せよ。
- /* pastr */
- #include
- main()
- {
- char *pastr[3];
- pastr[0] = "This is";
- pastr[1] = "That is";
- pastr[2] = "there are";
- printf("%s a pen.\n", *pastr);
- printf("%s a mirror.\n", *(pastr+1));
- printf("%s two tables in the room.\n", *(pastr+2));
- }
putchar を使って 文字列を順次出力するプログラムを作成せよ。
- /* outptarry */
- #include
- main()
- {
- char str[] = "aburakadabura";
- int i = 0;
- while (str[i] != '\0') {
- putchar(str[i]);
- i++;
- }
- printf("\n");
- }
(追記 上のプログラムは 6月12日に書いたコード calcstr に少し似ている)
文字列へのポインタ配列を利用して 3つの文章をランダムに 20行出力するプログラムを作成せよ。
- /* randarray */
- #include
- #include
- main()
- {
- char *str[3];
- int seed, i, j;
- str[0] = "ダメダメ ( ; _ ; ) !";
- str[1] = "トノ、御無体な . .";
- str[2] = "office タン 助けて !";
- printf("Input seed : ");
- scanf("%d", &seed);
- srand(seed);
- i = 0;
- while (i < 20) {
- j = rand() % 3;
- printf("%s\n", str[j]);
- i++;
- }
- }
(乱数をつくる関数 srand()と rand() はヘッダーファイルの
text: 金山典世さん(稚内北星)のページ