작은숲:위키노트/C 언어 예제/문자열 정렬
/**
* Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org>
* http://mooo.org
* ----------------------------------------------------------------------
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{ char *word= { "Welcome", "to", "Bitcamp", };
char *temp;
int i, j, k, num = 3;
for (i = 0; i < num; i++)
printf("Original Word %d: %s\n", i, word[i]);
for (i = 0; i < num - 1; i++) {
for (j = 0; j < num - 1; j++) {
for (k = 0; word[j][k] != '\0' && word[j + 1][k] != '\0'; k++) {
if (word[j][k] > word[j + 1][k]) {
temp = word[j];
word[j] = word[j + 1];
word[j + 1] = temp;
}
break;
}
}
}
for (i = 0; i < num; i++)
printf("Sorted Word %d: %s\n", i, word[i]);
return 0;
}
거품정렬을 이용한 예제이다.
temp = word[j];
word[j] = word[j + 1];
word[j + 1] = temp;