一段小程序(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);
}

 

你可能感兴趣的:(编程学习)