C实现字符串排序操作

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

#define N 20

void sort_data(char *const src,const unsigned int n)
{
	int i,j;
	for(i = 0; i < n - 1; i ++)
	{
		for(j = 0; j < n - i - 1; j ++ )
		{
			if(src[j] > src[j + 1])
			{
				src[j] ^= src[j + 1];
				src[j + 1] ^= src[j];
				src[j] ^= src[j + 1];
			}
		}
	}
}

int main(int argc, const char * argv[])
{
	char str_a[2 * N] = {0};
	char str_b[N] = {0};
	char *pa = str_a;
	char *pb = str_b;
	char ch;

	while(pa - str_a < N && '#' != (ch = getchar()) )
	{
		if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
			*pa ++ = ch;
	}
	while(pb - str_b < N && '#' != (ch = getchar()) )
	{
		if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
			*pb ++ = ch;
	}
	puts("-------------------------------");
	puts("the first string is :");
	puts(str_a);
	printf("length is :\t%d\n", pa - str_a);
	puts("-------------------------------");
	puts("the second string is :");
	puts(str_b);
	printf("length is :\t%d\n",pb - str_b);
	puts("-------------------------------");
	
	sort_data(str_a,pa - str_a);
	sort_data(str_b,pb - str_b);
	
	pa = str_a;
	pb = str_b;
	while(*pa != '\0')
		pa ++;
	while(*pb != '\0')
		*pa ++ = *pb ++;
	*pb = '\0';

	puts("the final string is :");
	puts(str_a);

    printf("length is :\t%d\n",pa - str_a);
	puts("-------------------------------");
    
	return 0;
}


测试结果

在这里插入图片描述

#include <stdio.h>

#define N 20

void sort_data(char *const src, const unsigned int n)
{
	int i,j;
	if(src == NULL || n < 1)
		return;
	for(i = 0; i < n - 1; i ++)
	{
		for(j = 0; j < n - i - 1; j ++ )
		{
			if(src[j] > src[j + 1])
			{
				src[j] ^= src[j + 1];
				src[j + 1] ^= src[j];
				src[j] ^= src[j + 1];
			}
		}
	}
}

unsigned int get_data(char *const dest, const unsigned int n )
{
	char ch;
	char *p = dest;
	if(dest == NULL || n < 1)
		return 0;
	while(p - dest < n && '#' != (ch = getchar()) )
	{
		if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
			*p ++ = ch;
	}
	return p - dest;
}

char * mystrcat(char *dest, const char * src)
{
	char *old = dest;
	if(dest == NULL || src == NULL)
		return NULL;
	while(*dest != '\0')
		dest ++;
	while(*src != '\0')
		*dest ++ = *src ++;
	*dest = '\0';
	return old;
}

void show_data(const char *str)
{
	const char *p = str;
	if(str == NULL)
		return;
	while(*p != '\0')
		p ++;
	printf("string:\t%s\n", str);
	printf("length:\t%d\n", p - str);
}

int main(int argc, const char * argv[])
{
	char str_a[2 * N] = {0};
	char str_b[N]     = {0};
	unsigned int len_a,len_b;
	len_a = get_data(str_a, N);
	len_b = get_data(str_b, N);
	show_data(str_a);
	show_data(str_b);
	sort_data(str_a,len_a);
	sort_data(str_b,len_b);

	mystrcat(str_a,str_b);
	show_data(str_a);
    
	return 0;
}


测试结果

在这里插入图片描述

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页