문자열 비교..

typ의 이미지

string 관련 함수(strlen, strcmp 등등)을 사용하지 않고,

문자열을 받아 비교하는 프로그램을 만들려 합니다.

문자열을 비교한후 정렬을 해야 하는데, 자꾸 에러가 나는구요 ㅜㅜ

일단 코드를 올립니다. 여기서 case sort : 부분에서

두 문자열을 순서를 swap 하는 부분만 좀 잡아주시면 고맙겠습니다.

#include <stdio.h>
#include <string.h>

#define nmax 21
#define read 1
#define sort 2
#define find 3
#define exit 4
#define true 5
#define false 6

void main()
{
	char keycode;
	char word[100][nmax];
	char *tempword;
	int phase=0, i, j,n, low, high, mid;
	int word_count = 0;
	int menu, found;
	int n_student = 0;
	int num_char=0;

	do {
		printf("\n\t 단어 찾기 프로그램 \n");
        printf("\n1. 단어 정보 입력 \n");
		printf("\n2. 단어  정렬 \n");
		printf("\n3. 단어 찾기/출력  \n");
		printf("\n4. 종료 \n");
		printf("\n\n\t 메뉴 항목을 입력하시오.\n");
		scanf("%d", &menu);
		switch (menu)
		{
		case read:
			printf("\n\t 입력할 단어수?");
			scanf("%d",&n);
			for (i=0;i<n;i++)
			{
			printf("\n\t word=");
			
			scanf(" %s", word + i);

			n_student++;
			printf("result: %s", word+i);
			}
			break;
		case sort:
		
			for (phase=1;phase<n_student;phase++)
			{
				
				for (i=0;i<n_student-phase;i++)
				{

					while( *(*(word+i)+num_char) != '\0')
						num_char++;

					printf("num_char :  %d\n", num_char);
/************************************
 * 이부분이 스왑부분입니다.!!!!!!!!!!!!!!!!!!!!!!!!
************************************/

					for(j=0 ; j< num_char; j++)  					{
						printf(" %c , %c\n", *(*(word+i) + j), *(*(word+i+1)+ j));
						if(*(*(word+i) + j) > *(*(word+i+1)+ j))
						{
						
							tempword = &word[i];


							
							&word[i] = &word[i+1];
							&word[i+1] = tempword;		

							break;
						}
					}
				}
			}

	//		for(i=0; i<n_student; i++)
	//			printf(" %c\n", *(*(word+i) + 1));
	//	for(i=0; i<n_student; i++)
	//	printf(" %s\n", word + i);
	//		for(i=0; i<n_student; i++)
	//			printf(" %s\n", word[i]);
			break;
	     case find:
			printf("\n enter the word to find=");
			while (scanf("%c", keycode) == 1){
				low=0;
				high = nmax+1;
				found = false;
				while (!found  && low<=high){
					mid=(low+high)/2;
					if (word[mid]==keycode)
						found=true;
					else if (word[mid]>keycode)
						high=mid-1;
					else 
						low=mid+1;
				}
				if (found)
					printf("%dth word is %s f\n", i+1, word[mid]);
				else
					printf("%dth word cannot found in the list. \n", i+1);
				printf("\n enter keycode=");
			}
			break;
		}
	} while (menu!=exit);
}
alsong의 이미지

&word[i] = &word[i+1]; 
&word[i+1] = tempword;  

char tempword[nmax];

strncpy(tempword, &word[i][0], strlen(&word[i][0]));
strncpy(&word[i][0], &word[i+1][0], strlen(&word[i+1][0]));
strncpy(&word[i+1][0], tempword, strlen(tempword));

'='이 복사(memcpy)를 하는 컴파일러가 존재하는것 같더군요.
어째든 tempword의경우는 공간이 필요하게 됩니다.
참고로 상수를 정의할때는 대문자를 많이 사용합니다. 참고하세요.
#define nmax 21 -> #define NMAX 21

그나저나 백수 언제 탈출하냐... ㅡㅡ; 배고파라.