c语言关于字符串处理

polar_1995 2018-10-17 06:40:43
设计一程序实现功能,处理字符串A,处理规则是:只要B字符串里面有的字母,不分大小写,一律从A字符串中删掉。
(1)请画出此算法的流程图;
(2)请用C语言编写对应的代码。
求大神帮帮我
...全文
247 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
super_admi 2018-10-18
  • 打赏
  • 举报
回复
引用 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);
}
weixin_41421170 2018-10-18
  • 打赏
  • 举报
回复
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");
}


wodeyouxian 2018-10-18
  • 打赏
  • 举报
回复
我去,上面这复杂度,咋都是 n的平方呢?就不能降到n?采用查表的方法,可以降到n的吧? ======================= 有代码就发出来看看吧
super_admi 2018-10-18
  • 打赏
  • 举报
回复
我去,上面这复杂度,咋都是 n的平方呢?就不能降到n?采用查表的方法,可以降到n的吧?
super_admi 2018-10-18
  • 打赏
  • 举报
回复
给个最终版本:

#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);
}
wodeyouxian 2018-10-17
  • 打赏
  • 举报
回复
#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; }
みしつかん 2018-10-17
  • 打赏
  • 举报
回复
大体框架给你了,基本的功能也都实现了。运行已通过,你再根据需要自己改改就好了


#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");
}
  • 打赏
  • 举报
回复
第一种: 从a[0]开始,逐个到b里面找,找到,则删除。 这种算法的时间复杂度是ax*bx 第二种: 从b[0]开始,逐个到a里面找,找到则删除a里面的。 时间复杂度比上面的那个稍微小一些。
polar_1995 2018-10-17
  • 打赏
  • 举报
回复
不太懂啊,楼上的
AlbertS 2018-10-17
  • 打赏
  • 举报
回复
真的不想写完整代码,只提供一个思路,主要用到strstr,首先需要保存A字符串为AA,然后把A字符串和B字符串都转换为大写或者小写,利用strstr函数在A中找到包含B的所有位置,利用所有找到的位置和B串的长度,在AA中截取相应的部分,然后拼接在一起就可以

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧