利用指针来进行字符串排序

/*************************************************************************
	> File Name: strswap.c
	> Author: Jam
	> Mail: [email protected] 
	> Created Time: 2018年07月29日 星期日 09时19分46秒
 ************************************************************************/

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

#define row 3 
#define list 20 

void input ();

int main()
{
	char str[row][list] = {"C++","Html","Java"};
	
	char *p[row];
	
	int i = 0;
	int j = 0;
	char *temp = NULL;
	
	for(i = 0;i < row; i++)
	{
		p[i] = str[i];   
	}
	
	for(i = 0; i < row-1; i++)//该处利用了选择排序
	{	
		for(j = i+1; j < row; j++)
		{
			if(strcmp(p[i],p[j]))
			{
				temp = p[i];
				p[i] = p[j];
				p[j] = temp;
			}
		}
	}
	
	printf("str字符串数组中:  ");
	for(i = 0; i < row; i++)
	{
		printf("%s\t",str[i]);
	}
	printf("\n");
	printf("p指针数组中:  ");
	for(i = 0; i < row; i++)
	{
		printf("%s\t",p[i]);
	}
	printf("\n");
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40949398/article/details/81270266