字符串數組按照字母序排序(C++)

//一個字符串數組按照字母序排序

#include<iostream>
#include<string.h> 
using namespace std;

int main()
{
	char * arr[]= {"hello", "world", "this", "is"};
	int len = (sizeof(arr)/sizeof(char*));
	
	int i,j;
	for(i=0;i<len;i++)
	{
		for(j=0;j<len-1-i;j++)
		{
			if((arr[j][0] - arr[j+1][0]) > 0 )
			{
				char *temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
	}
	
	for(int n=0;n<len;n++)
	{
		cout<<arr[n]<<endl;
	}

        return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章