strDivide2.cpp字符串划分

本文详细阐述了如何解析包含字母的字符数组,并实现基于字母顺序的排序操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// strDivide2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
/*
s为 bwe@#$at111YYY*oo
那么func(s)将打印
at
bwe
oo
YYY

★树★(240028358)  21:07:57
先挑字母,再排序吧
国嵌唐老师(22134670)  21:21:25
我来说说 怎么做
1. 要先判断各个单词位置 把其它字符清成0, 这样字符数组中的就存放的是单词 组合 每个单词以0结束
2. 申请一个字符指针数组  将每个元素指向不同的单词
3. 写一个函数对这个指针数组排序

  唐老师的微博地址是多少啊?
http://weibo.com/u/1234587177

*/
bool isLowChar(char p)
{
	return (p>='a' && p<='z') ;
}
bool isUpChar(char p)
{
	return (p>='A' && p<='Z');
}

bool isChar(char p)
{
	return (p>='a' && p<='z') || (p>='A' && p<='Z');
}


void func(char *s)
{
	char* strArray[50];
	memset(strArray,0,sizeof(strArray));

	int strIndex=-1;
	char *p=s;
	bool isNewword=true;

	while(*p!='\0')
	{
	
		if(isChar(*p))
		{
			if(isNewword)
			{
				strIndex++;
				strArray[strIndex]=p;
				isNewword=false;
			}			
		}//if(isChar(*p))
		else
		{
			*p='\0';
			isNewword=true;
		}

		p++;
	}//while(*p!='\0')

	//排序

	for(int i=0;i<strIndex;i++)
	{
		for(int j=i+1;j<=strIndex;j++)
			if((isLowChar(*strArray[i]) && isLowChar(*strArray[j])) || (isUpChar(*strArray[i]) && isUpChar(*strArray[j])) )
				if(*strArray[i] > *strArray[j])
				{
					char *ptmp=strArray[i];
					strArray[i]=strArray[j];
					strArray[j]=ptmp;
				}
			else if(isUpChar(*strArray[i]))
				{
					char *ptmp=strArray[i];
					strArray[i]=strArray[j];
					strArray[j]=ptmp;
				}

	}
//打印
	for( i=0;i<strIndex;i++)
	{
		printf("%s\n",strArray[i]);
	}

}


int main(int argc, char* argv[])
{
	char s[]="bwe@#$at111YYY*oo";

	func(s);
	return 0;
}

/*
at
bwe
YYY
Press any key to continue
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值