190104作业-将包含字符数字的字符串分开

版权声明:版权归属tangobravo所有 https://blog.csdn.net/tangobravo/article/details/85886859

将包含字符数字的字符串分开,使得分开后的字符串前一部分是数字后一部分是字母。例
如“
h1ell2o3” ->”123hello”

/*不考虑可以使用额外空间的情况是很好完成,如何实现原地分离??值得思考。

还可以考虑数字是无序的情况。*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
void Alpha_Number_Sort()
{
	char ch[MAX];
	char *p;
	char *pend;
	while (gets_s(ch, MAX) != NULL)
	{
		p = ch;
		int size = strlen(ch);
		pend = ch + size - 1;
		char *ch1 = (char *)malloc(size);
		char *p1 = ch1;
		char *p1end = ch1 + size - 1;
		while (p <= ch + size - 1 && pend >= ch)
		{
			if (*p >= '0'&&*p <= '9')
				*p1++ = *p;
			if (*pend<'0' || *pend>'9')
				*p1end-- = *pend;
			++p;
			--pend;
		}
		ch1[size] = '\0';
		puts(ch1);
	}
}

int main()
{
	Alpha_Number_Sort();
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/tangobravo/article/details/85886859