抽离字符串中字符和数字,并不改变原来的字母数字之间顺序再组合

本文介绍了一个使用C++进行字符串处理的示例程序,通过分离字母和数字字符,展示了如何利用C++标准库函数实现字符串的拆分和重组。代码中包含了字符分配、条件判断和字符串操作等关键步骤。

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

// test.cpp : 定义控制台应用程序的入口点。
//

#define _CRT_SECURE_NO_WARNINGS

#include "stdafx.h"

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;


char * apple1(char * str){
	char * Body = (char *)malloc(strlen(str) + 1);
	char * tmp = (char *)malloc(strlen(str) + 1);
	char * tmpnum = (char *)malloc(strlen(str) + 1);
	char * start = tmp;
	char * numstart = tmpnum;

	while (*str != '\0') {
		if ((*str >= 'A'&&*str <= 'Z') || (*str >= 'a'&&*str <= 'z')) {
			*tmp = *str;
			tmp++;
		}
		else if (*str >= '0'&&*str <= '9'){
			*tmpnum = *str;
			tmpnum++;
		}
		str++;
	}

	*tmp = '\0';
	*tmpnum = '\0';
	cout << start << endl;
	cout << numstart << endl;
	strcpy_s(Body, strlen(Body), start);
	strcat(Body, numstart);
	cout << Body << endl;
	free(start);
	free(numstart);

	return Body;
}




int _tmain(int argc, _TCHAR* argv[])
{
	char * str = "1919china";
	char * dst1 = (char *)apple1(str);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值