LoginSignup
12
11

More than 3 years have passed since last update.

Cで湯婆婆を実装してみる

Last updated at Posted at 2020-11-07

元ネタ

↓が流行っているらしいので便乗してみました。
@NemesisさんのJavaで湯婆婆を実装してみる

コード

Cの場合環境依存性が高いので、paiza.ioでやってみました。

2020/11/8編集:
 未入力と空白一文字だけの場合は再入力するようにしてみました。
 原作リスペクトを考えると落ちたほうが良いのか・・・?

yubaba.c
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    char name[65];/*4byte×16+'\0'*/
    int first_char_len=0;

    setlocale(LC_ALL, "");
    memset((void*)name,0,sizeof(name));

    printf("契約書だよ。そこに名前を書きな。\n");
    while(1){
        scanf("%64s%*[^\n]", name);
        if((name[0]!='\0') && (strcmp(name," ")!=0) && (strcmp(name," ")!=0)){
            break;
        }
        printf("[あなた]> あの、名前ってここですか?\n\n"   
               "そうだよもぅぐずぐずしないでさっさと書きな!\n"
               "まったく……つまらない誓いをたてちまったもんだよ。働きたい者には仕事をやるだなんて……\n"
               "書いたかい?\n\n");    
    }
    printf("フン。%sというのかい。贅沢な名だねぇ。\n",name);

    first_char_len = mblen(name, sizeof(name));
    name[first_char_len] = '\0';

    printf("今からお前の名前は%sだ。いいかい、%sだよ。分かったら返事をするんだ、%s!!",name,name,name);
}

参考サイト

setlocale
http://www.c-lang.org/detail/function/setlocale.html

【C言語】scanf関数で安全に文字列を読み込む方法
https://marycore.jp/prog/c-lang/scanf-string-safely/

mblen
http://www.c-lang.org/detail/function/mblen.html

12
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11