一段小程序(3)

做个题目练练手,不然C语言都忘记了。

设计一程序实现功能,处理字符串A,处理规则是:只要B字符串里面有的字母,不分大小写,一律从A字符串中删掉。
(1)请画出此算法的流程图;
(2)请用C语言编写对应的代码。

 

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define VAL_ASCII_SIZE 256
#define FALSE          0
#define TRUE           1
typedef unsigned char byte;

char* getWellChars(const char* dataChars, const char* dictChars, int caseSensitive) {
	byte markAscii[VAL_ASCII_SIZE] = {'\0'};
	int dataIndex = 0;
	int dataCount = strlen(dataChars);
	char* wellChars = (char*)malloc(sizeof(char)*dataCount);
	int dictIndex = 0;
	int dictCount = strlen(dictChars);
	int skipCount = 0;
	
	memset(wellChars, 0, sizeof(char)*dataCount);

	while(dictIndex < dictCount) {
		char dictChar = dictChars[dictIndex];
		markAscii[dictChar] = 1;
		if(!caseSensitive) {
			if ('A' <= dictChar && dictChar <= 'Z') {
				markAscii[dictChar | 0x20] = 1; //0010 0000, to lowercase
			}
			else if ('a' <= dictChar && dictChar <= 'z') {
				markAscii[dictChar & 0xDF] = 1; //1101 1111, to uppercase
			}
		}
		++dictIndex;
	}

	while(dataIndex < dataCount) {
		char dataChar = dataChars[dataIndex];
		if(!markAscii[dataChar]) {
			wellChars[dataIndex - skipCount] = dataChar;
		} else {
			++skipCount;
		}
		++dataIndex;
	}
	return (wellChars);
}

int main() {
	int status = 0;
	char* data = "aAbBcChHzZaAbBcCXxOo";
	char* dict = " @aAaaabBbbbcCcccDDDEEFFo";
	char* well = NULL;

	printf("data:%s\n", data);
	printf("dict:%s\n", dict);
	well = getWellChars(data, dict, FALSE);
	printf("well:%s\n", well);
	free(well);
	
	return (status);
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值