ホームページへ戻る
 

上へ戻る
 

  C言語入門     サンプルコード 2
BY H.Y
2月 25 15:33:11 JST 2002
目次
1. sort.c
2. toup.c


// 1. ------------ sort.c ------------------------------
#include <stdio.h>

#define MAX 8
#define LMAX 100

char *name[]={"Yoshinouchi","Tanaka","Hatanaka","Ogawa",
"Okada","Nakamoto","Yamamoto","Iwaki"};
char buf[MAX][LMAX];

int main(){
int n, i, j;
char tmp[128];


for(i=0; i<MAX; ++i)
strcpy(buf[i],name[i]);

for(i=0; i<MAX; ++i)
printf("%d : %s\n",i, buf[i]);
printf("\n");

n = MAX -1;
for(i=0; i<n; ++i){
for(j = i + 1; j<MAX; ++j){
if(strcmp(buf[i],buf[j]) > 0){
strcpy(tmp,buf[i]);
strcpy(buf[i],buf[j]);
strcpy(buf[j],tmp);
}
}
}
for(i=0; i<MAX; ++i)
printf("%d : %s\n",i, buf[i]);

return 0;
}

// 2. ------------ toup.c ------------------------------
#include <stdio.h>
#include <ctype.h>

#define MAX 8
#define LMAX 100

char *name[]={"Yoshinouchi","Tanaka","Hatanaka","Ogawa",
"Okada","Nakamoto","Yamamoto","Iwaki"};
char buf[MAX][LMAX];

int main(){
int n, i, j;
char tmp[128];

for(i=0; i<MAX; ++i)
strcpy(buf[i],name[i]);

for(i=0; i<MAX; ++i)
printf("%d : %s\n",i, buf[i]);
printf("\n");

//--- to upper ------------------------
for(i=0; i<MAX; i++){
for(j=0; buf[i][j] != 0; j++)
buf[i][j] = toupper((int)buf[i][j]);
}
//--------------------------------------
n = MAX -1;
for(i=0; i<n; ++i){
for(j = i + 1; j<MAX; ++j){
if(strcmp(buf[i],buf[j]) > 0){
strcpy(tmp,buf[i]);
strcpy(buf[i],buf[j]);
strcpy(buf[j],tmp);
}
}
}
for(i=0; i<MAX; ++i)
printf("%d : %s\n",i, buf[i]);

return 0;
}