user-0 提问: c语言关于字符串处理

回复:
user: user-1
content: 引用 7 楼 wodeyouxian 的回复:我去，上面这复杂度，咋都是 n的平方呢？就不能降到n？采用查表的方法，可以降到n的吧？ ======================= 有代码就发出来看看吧  #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif  #include "stdio.h" #include "stdlib.h" #include "string.h"  #define VISIBLE_CHAR_SIZE 95 #define VISIBLE_CHAR_GAPS 32  char* reject(const char* dataString, const char* dictString) { 	char markString[VISIBLE_CHAR_SIZE] = {'\0'}; 	int dataIndex = 0, dataLength = strlen(dataString), dictIndex = 0, dictLength = strlen(dictString), skipCount = 0; 	char* wellString = (char*)malloc(sizeof(char)*dataLength); 	memset(wellString, 0, sizeof(char)*dataLength);  	while(dictIndex < dictLength) 	{ 		char dictChar = dictString[dictIndex]; 		int markIndex = dictChar - VISIBLE_CHAR_GAPS; 		markString[markIndex] = 1; 		if ('z' >= dictChar && dictChar >= 'a') 		{ 			markString[markIndex - VISIBLE_CHAR_GAPS] = 1; 		} 		else if ('Z' >= dictChar && dictChar >= 'A') 		{ 			markString[dictChar] = 1; 		} 		++dictIndex; 	}  	while(dataIndex < dataLength) 	{ 		char dataChar = dataString[dataIndex]; 		int markIndex = dataChar - VISIBLE_CHAR_GAPS; 		if(!markString[markIndex]) 		{ 			wellString[dataIndex - skipCount] = dataChar; 		} 		else 		{ 			++skipCount; 		} 		++dataIndex; 	} 	return (wellString); }  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 = reject(data, dict); 	printf("well:%s\n", well); 	free(well); 	return (status); }

user: user-2
content: 1、流程      （1）将B串中的字母统一按小写字母建立26个字符集的Hash表；      （2）遍历A字符串，如果是字母，则按小写字母查找Hash表中是否存在；如果存在，删除A字符串中的这个字符； 2、代码 #include "stdio.h" #include "string.h" #include "ctype.h"  bool isCharExist(char c, int cHash[]) { 	if(!isalpha(c))	return false;  	int lowChar = tolower(c)-'a'; 	return cHash[lowChar] == 1; }  void makeHash(char *strb, int cHash[]) {  	for(int i = 0; i < strlen(strb); i++) 	{ 		if(!isalpha(strb[i])) continue;  		int lowChar = tolower(strb[i])-'a'; 		cHash[lowChar] = 1; 	} }   void main() { 	int cHash[26]; 	char strb[80]; 	char stra[80];  	printf("\ninput string B: "); 	scanf("%s", strb);  	printf("\ninput string A: "); 	scanf("%s", stra);  	memset(cHash, 0, sizeof(cHash)); 	makeHash(strb, cHash);  	int len = strlen(stra); 	for(int i = 0; i < len; ) 	{ 		if(isCharExist(stra[i], cHash)) 		{ 			for (int j = i; j  < len; j++)		 				stra[j] = stra[j + 1];			 			len --; 		} 		else 			i++; 	}  	printf("\nstra:%s", stra); 	printf("\n\n"); }

user: user-3
content: 我去，上面这复杂度，咋都是 n的平方呢？就不能降到n？采用查表的方法，可以降到n的吧？ ======================= 有代码就发出来看看吧

user: user-1
content: 我去，上面这复杂度，咋都是 n的平方呢？就不能降到n？采用查表的方法，可以降到n的吧？

user: user-1
content: 给个最终版本：  #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); }

user: user-3
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  int chuli(char * a, char * b) {   	int retCode = 0; 	char myA[100]; 	char myB[100]; 	char jieguo[100]; 	char * p = NULL; 	int i = 0 , j = 0; 	    	 	//验证输入的参数是否正确 	if(a== NULL || b == NULL) 	{ 		retCode = -1; 		printf("function chuli's args are wrong!, return code is %d\n",retCode); 		goto EndSubFun; 	}      //函数功能块  	memset(myA, 0 ,100); 	memset(myB, 0 ,100); 	memset(jieguo,0,100); 	p = jieguo;  	strcpy(myA, a); 	strcpy(myB, b);  	for(i = 0 ; i< 100;i++) 	{ 		if(myB[i] >= 'A' && myB[i] <= 'Z') 			myB[i] = myB[i] + 32; 	} 	for(i = 0 ; i< 100;i++) 	{ 		if(myA[i] >= 'A' && myA[i] <= 'Z') 			myA[i] = myA[i] + 32; 	} 	 	for(i = 0 ; i< 100; i++) 	{ 		for(j=0 ; j< 100; j++) 		{ 			if(myA[i] == myB[j]) 				myA[i] = '!'; 		} 	}  	for(i = 0 ; i< 100; i++) 	{ 		if(myA[i] != '!') 		{ 			*p++ = *(a+i); 		} 	}  	printf("%s\n",jieguo); 		    	p = NULL;	 	memset(myA, 0 ,100); 	memset(myB, 0 ,100); 	memset(jieguo,0,100);  EndSubFun:     	  		return retCode; }  int main(int argc , char * argv[]) { 	int ret = 0;  	char  a[]="testmyatbadeaRRSsdtbABCdegc"; 	char  b[]="aBc"; 	 	chuli(a,b);  	return ret; }

user: user-4
content: 大体框架给你了，基本的功能也都实现了。运行已通过，你再根据需要自己改改就好了   #include<iostream> using namespace std;  #include<string> using  std::string;  char a[6]={ 'a', 'b', 'c', 'd', 'e', 'f'};   //删除a中b有的字符 char b[3] = {  'b', 'd', 'f' }; int len1 = strlen(a);  //计算字符数组a的长度 int len2 = strlen(b); // 计算字符数组b的长度  int main() { 	for (int i=0;i<len2;i++)   //将b中的每个字符和a中的字符比较 	{ 		for (int j = 0; j < len1; j++) 		{ 			if (b[i] == a[j]) 			{ 				for (int k = j; k < len1; k++) 				a[k] = a[k + 1];     //用a中的后一个字符替换它  			} 		}	 	} 	cout << a << endl; 	system("pause"); }

user: user-5
content: 第一种： 从a[0]开始，逐个到b里面找，找到，则删除。 这种算法的时间复杂度是ax*bx 第二种： 从b[0]开始，逐个到a里面找，找到则删除a里面的。 时间复杂度比上面的那个稍微小一些。

user: user-0
content: 不太懂啊，楼上的

user: user-6
content: 真的不想写完整代码，只提供一个思路，主要用到strstr，首先需要保存A字符串为AA，然后把A字符串和B字符串都转换为大写或者小写，利用strstr函数在A中找到包含B的所有位置，利用所有找到的位置和B串的长度，在AA中截取相应的部分，然后拼接在一起就可以