【課題4−3】

 

(プログラム例1)

#include <stdio.h>

#include <string.h>

#define N 12

int main(void) {

         char *month[N]={"January", "February", "March", "April",

                          "May", "June", "July", "August",

                          "September", "October", "November", "December"};

         int i;

 

         for (i = 0; i < N; i++) {

                 printf("length of \"%s\": %d\n", month[i], strlen(month[i]));

         }

         return 0;

}

 

(実行結果)

length of "January": 7

length of "February": 8

length of "March": 5

length of "April": 5

length of "May": 3

length of "June": 4

length of "July": 4

length of "August": 6

length of "September": 9

length of "October": 7

length of "November": 8

length of "December": 8

 

 

(プログラム例2)

#include <stdio.h>

#include <string.h>

#define N 12

void sort1(char *dat[]);

void sort2(char *dat[]);

int main(void) {

         char *month[N]={"January", "February", "March", "April",

                          "May", "June", "July", "August",

                          "September", "October", "November", "December"};

         int i;

 

         sort1(month);  //辞書順でのソート

         sort2(month);  //文字数でのソート

 

         for (i = 0; i < N; i++) {

                 printf("%s\n", month[i]);

         }

         return 0;

}

 

/* 基本挿入法で文字列を辞書順にソートする関数 */

void sort1(char *dat[]) {

         int i, j;

         char *temp;

        

         for (i = 0; i < N; i++) {

                 for (j = i-1; j >= 0; j--) {

                          if (strcmp(dat[j], dat[j+1]) > 0) {

                                  temp = dat[j];

                                  dat[j] = dat[j+1];

                                  dat[j+1] = temp;

                          } else

                                  break;

                 }

         }

}

 

/* 基本挿入法で文字列を長さ順にソートする関数 */

void sort2(char *dat[]) {

         int i, j;

         char *temp;

        

         for (i = 0; i < N; i++) {

                 for (j = i-1; j >= 0; j--) {

                          if (strlen(dat[j]) > strlen(dat[j+1])) {

                                   temp = dat[j];

                                  dat[j] = dat[j+1];

                                  dat[j+1] = temp;

                          } else

                                  break;

                 }

         }

}

 

(実行結果)

May

July

June

April

March

August

January

October

December

February

November

September

 

 

(プログラム例3)標準ライブラリ関数qsortを用いた例

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 12

int cmpstr(const void *, const void *);

int cmplen(const void *, const void *);

int main(void) {

         char *month[N]={"January", "February", "March", "April",

                          "May", "June", "July", "August",

                          "September", "October", "November", "December"};

         int i;

 

         qsort(month, N, sizeof(char *), cmpstr); //辞書順でのソート

         qsort(month, N, sizeof(char *), cmplen); //文字数でのソート

 

         for (i = 0; i < N; i++) {

                 printf("%s\n", month[i]);

         }

         return 0;

}

 

/* 2つの文字列の辞書順を比較する関数 */

int cmpstr(const void *s1, const void *s2) {

         return strcmp(*(char **)s1, *(char **)s2);

}

 

/* 2つの文字列の長さを比較する関数 */

int cmplen(const void *s1, const void *s2) {

         return strlen(*(char **)s1)-strlen(*(char **)s2);

}

 

(実行結果)

May

July

June

April

March

August

January

October

December

February

November

September