数据结构——算法(030)(在所有小写字母串大写字母前排)

【状态:本文仅限于自我总结和互动。还有希望你指出缺点。 联系邮箱:Mr_chenping@163.com】

题目:

有一个由大写和小写组成的字符串,如今须要对他进行改动,将当中的全部小写字母排在大写字母的前面

开辟足够少的空间,时间复杂度O(n)
题目分析:

1、字符串从新排序后没有要求保持曾经的顺序

2、用两个指针分别指向字符串头和尾。头指针指向每次指向大写字符,尾指针每次指向小写字母,然后交换两个指针内容就可以

算法实现:

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

void str_proc(char *str)
{
	int len = strlen(str);
	char *start = str;
	char *end = str + len - 1;

	char temp;
	while(start < end)
	{
		if(*start >= 'a' && *start <= 'z')
		{	
			start++;
			continue;
		}

		if(*end >= 'A' && *end <= 'Z')
		{
			end--;
			continue;
		}

		{
			temp = *start;
			*start++ = *end;
			*end-- = temp;
		}
	}
}

int main(int argc, char *argv[])
{
	printf("%s----->", argv[1]);
	str_proc(argv[1]);
	printf("%s
", argv[1]);
	return 0;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4627864.html