一、题目描述
给1000个人名字符串,如:raina tina anne....将这些人名按字典顺序排序
二、解题思路
字典顺序就是按a b c d顺序,对字符串排列,可以直接用strcmp实现
三、解题算法
/*****************************************
author:tmw
date:2018-8-26
******************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap( char* str1, char* str2 )
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = len1>len2?len1:len2;
char* temp = (char*)malloc(len*sizeof(char));
temp = strcpy(temp, str1);
str1 = strcpy(str1, str2);
str2 = strcpy(str2, temp);
free(temp);
}
void dictionary_sort( char** str, int str_len )
{
if( str == NULL ) return;
int i,j;
for(i=0; i<str_len; i++)
{
for(j=i+1; j<str_len; j++)
{
/**前面人名的字典序大于后面人名的字典序--换位置**/
if( strcmp(str[i], str[j]) > 0 )
swap(str[i], str[j]);
}
}
/**按字典序输出**/
for(i=0; i<str_len; i++)
printf("%s ",str[i]);
}
梦想还是要有的,万一实现了呢~~~加油~~ヾ(◍°∇°◍)ノ゙~~~~~~~~~~~~~~~~