문자열 비교..

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

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

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.