C言語 初心者です。
今、英単語帳を作っているのですが、以下のソースではできません。
作ろうとしているプログラムは、a bを登録した場合、次がaabと来たら、
a aab bといったようにしたいのですが、できません。教えてください。
#include <stdio.h>
#include <string.h>
#define NUMBER 50
/*--- 単語帳の構造体*/
typedef struct {
char *word;
} words;
/*--- 文字列strから文字列wordを検索する ---*/
char *str_chr(const char *str, int w)
{
for ( ; *str; *str++){
if (*str == w){
return ((char *)str);
}
}
return (NULL); /*検索したが該当しないときはNULLを返す*/
}
/*--- 単純交換ソート ---*/
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
/*--- 配列dataの先頭n個の要素を昇順にソート ---*/
void sort(words data[], int n)
{
int k = n - 1;
while (k >= 0){
int i, j;
for (i = 1, j = -1; i <= k; i++)
if (data[i - 1].word > data[i].word){
j = i - 1;
swap(&data[i], &data[j]);
}
k = j;
}
}
int main(void)
{
words word[NUMBER][20] = {{0},{0}};
char str[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char w[128], *p;
int count = 0;
do{
printf("単語を入力してください。:"); /*単語を入力する*/
scanf("%s", w);
p = str_chr(str, w);
}while(p == NULL);
count++;
if(count >= NUMBER){ /*登録件数を調べる*/
printf("件数いっぱいです。\n");
}
return (0);
sort(word, NUMBER);
return (0);
}
お礼
違いがよくわかり、助かりました。 ありがとうございました。