1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語初心者の学習記録

1
Last updated at Posted at 2025-09-21

はじめに

今回はC言語の学習記録として探索について学びました。
学習記録として随時記載していきます。
特段の目的等はありませんので、ご了承ください

実行環境

・windows:Windows11Pro 23H2

課題1

関数ポインタ、文字列配列を題材にした問題

test.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef enum {NO,YES} boolean;
typedef struct WordOps{
    void (*to_large)(char *);
    void (*to_small)(char *);
    void (*to_reverse)(char *);
}WordOps;

typedef struct Word{
    char text[32];
    WordOps *ops;
}Word;

void large(char *s);
void small(char *s);
void reverse(char *s);
void call_method(Word *w,int method);

void large(char *s){
    printf("before :%s\n",s);
    for(int i = 0;s[i]!= '\0';i++){
        s[i] = toupper((unsigned char)s[i]);
    }
    printf("after :%s\n",s);
}

void small(char *s){
    printf("before :%s\n",s);
    for(int i = 0;s[i]!= '\0';i++){
        s[i] = tolower((unsigned char)s[i]);
    }
    printf("after :%s\n",s);
}

void reverse(char *s){
    printf("before :%s\n",s);
    for(int i = 0;s[i] != '\0';i++){
        unsigned char c = s[i];
        if(isupper(c)){
            s[i] = tolower(c);
        }else if(islower(c)){
            s[i] = toupper(c);
        }
    }
    printf("after :%s\n",s);
}

void call_method(Word *w,int method){
    switch(method){
        case 0:w->ops->to_large(w->text);break;
        case 1:w->ops->to_small(w->text);break;
        case 2:w->ops->to_reverse(w->text);break;
    }
}
int main(){ 
    int select_func = 0;
    boolean select_flag = NO;
    WordOps ops = {large,small,reverse};
    Word words[] = {
        {"Apple",&ops},
        {"Banana",&ops},
        {"Cherry",&ops},
        {"Durian",&ops},
    };
    int words_num = sizeof(words)/sizeof(words[0]);

    while(select_flag == NO){
        printf("select func 0:LARGE,1;SMALL,2:REVERSE\n");
        scanf("%d",&select_func);
        if(0 <= select_func && select_func <= 2){
            select_flag = YES;
        }
    }

    if (select_func == 0)printf("YOUR SELECTION is LARGE\n");
    if (select_func == 1)printf("YOUR SELECTION is SMALL\n");
    if (select_func == 2)printf("YOUR SELECTION is REVERSE\n");

    for (int i = 0;i < words_num;i++)call_method(&words[i],select_func);

    return 0;
}
1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?