将一个输入字符串按照字符从小到大的顺序输出,并且剔除相同的字符串

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

/*
* @brief str_process    将输入的字符串中重复的部分剔除,并且排序输出
* @param src_str		输入的源字符串
* @param dest_str		处理后输出字符串
* @return 成功:0;失败: -1。
*/
int process_str(char *src_str, char* dest_str)
{
	int i = 0;
	int j = 0;
	char temp;
	int str_len = 0;
    int min = 0;

	/*第一步:对输入源字符串进行从小到大的排序,第一是冒泡排序,第二是选择排序*/
	str_len = strlen(src_str);

	printf("str_len:%d\n",str_len); 

#if 0
	for(i = 0; i < str_len - 1; i++)
	{
		for(j = 0; j < str_len - 1- i; j++)
		{
			if(src_str[j] > src_str[j + 1])
			{
				temp		   = src_str[j];
				src_str[j]	   = src_str[j + 1];
				src_str[j + 1] = temp;
			}
		}
	}
#endif

	for(i = 0; i < str_len; i++)
	{
		min = i;
		for(j = i+1; j < str_len; j++ )
		{
			if(src_str[min] > src_str[j])
			{
				min = j;
			}
		}

		if(min != i)
		{
			temp   = src_str[min];
			src_str[min] = src_str[i];
			src_str[i]   = temp;
		}
	}

	printf("now we check src_str:%s\n",src_str);
	
	/*第二步:去掉相同的字符*/
	for(i = 0,j = 0; i < str_len; i++)
	{
		if(src_str[i] != src_str[i + 1])
		{
			dest_str[j++] = src_str[i];
		}
	}

	printf("now we check dest_str:%s\n",dest_str);
	return 0;
}




int main(void)
{
	char test_src_str[100];
	char test_dest_str[100];
	
	memset(test_dest_str,0,100);

	gets(test_src_str);

	process_str(test_src_str, test_dest_str);

	getch();

	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值