二位字符数组的排序。
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char c[5][10] = {"zabd", "fadkfjdkb", "fadkfjdka", "egaoba", "beyond"};
int count = sizeof(c) / 10;
for(int i = 0; i < count - 1; i++)
{
int k = i;
for(int j = i + 1; j < count; j++)
{
if(strcmp(c[i] , c[j]) > 0)
{
k = j;
}
}
if(i != k)
{
char (*str)[10];
str = c[k];
c[k] = c[i];
c[i] = str;
}
}
for(int i = 0; i < count; i++)
{
printf("%s\n", c[i]);
}
printf("\ncount = %d\n", count);
printf("\n");
system("pause");
return 0;
}
void str_sort()
{
char c[5][10] = {"zabd", "fadkfjdkb", "fadkfjdka", "egaoba", "beyond"};
int count = sizeof(c) / 10;
for(int i = 0; i < count - 1; i++)
{
int k = i;
for(int j = i + 1; j < count; j++)
{
if(strcmp(c[i] , c[j]) > 0)
{
k = j;
}
}
if(i != k)
{
char str[10];
strncpy(str, c[k], 10);
strncpy(c[k], c[i], 10);
strncpy(c[i], str, 10);
}
}
for(int i = 0; i < count; i++)
{
printf("%s\n", c[i]);
}
printf("\ncount = %d\n", count);
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char c[][10] = { "zabd", "fadkfjdkb", "fadkfjdka", "egaoba", "beyond", "meyond", "oeper" };
int count = sizeof(c) / sizeof(c[0]);
for (int i = 0; i < count - 1; i++)
{
int k = i;
for (int j = i + 1; j < count; j++)
{
if (strcmp(c[k], c[j]) > 0)
{
k = j;
}
}
if (i != k)
{
char str[10];
strncpy(str, c[k], 10);
strncpy(c[k], c[i], 10);
strncpy(c[i], str, 10);
}
}
for (int i = 0; i < count; i++)
{
printf("%s\n", c[i]);
}
printf("\ncount = %d\n", count);
printf("\n");
system("pause");
return 0;
}