用思维导图和实例学习C之一字符串处理文档格式.docx
- 文档编号:17022264
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:8
- 大小:22.64KB
用思维导图和实例学习C之一字符串处理文档格式.docx
《用思维导图和实例学习C之一字符串处理文档格式.docx》由会员分享,可在线阅读,更多相关《用思维导图和实例学习C之一字符串处理文档格式.docx(8页珍藏版)》请在冰豆网上搜索。
"
endl;
cout"
strA:
strAendl;
strB:
strBendl;
strC:
strCendl;
strD:
strD"
\n\n"
//DisplaythelengthofstrA.cout"
LengthofstrAis"
strlen(strA)endl;
SizeofstrAis"
sizeof(strA)endl;
//ConcatenatestrBwithstrAcout"
TheresultofConcatenateisstrA:
//CopystrCintostrB,andpartiallystrDintostrAcout"
TheresultofCopyis:
TheresultofpartiallyCopyisstrA:
//ComparestrCwithstrBif(!
strcmp(strC,strB)){cout"
strCisequaltostrB!
if(!
strncmp(strD,strA,3)){cout"
strDisequaltostrApartially!
return0;
Ex2.搜索匹配相关操作
2-2.cpp**Description:
SearchOperationinCtypeString**Version:
05/11/201011:
38:
15AM*Revision:
IBMCDL**===*/
#include#includeusingnamespacestd;
intmain(void){constchar*url="
HerbS"
constchar*url2="
Apache.org"
constchar*emailaddr="
Herb@HerbS"
constchar*tld={"
.com"
"
.net"
.org"
};
constchar*p;
//First,determineifurlandurl2,.net,or.org.for(inti=0;
i3;
i++){p=strstr(url,tld[i]);
if(p)couturl"
hastop-leveldomain"
tld[i]endl;
p=strstr(url2,tld[i]);
if(p)couturl2"
}//Searchforaspecificcharacter.p=strchr(emailaddr,'
@'
);
if(p)cout"
Sitenameofe-mailaddressis:
p+1endl;
//Searchforanyofasetofcharacters.Inthiscase,//findthefirst@orperiod.p=strpbrk(emailaddr,"
@."
Found"
*pendl;
//Searchforthebeginningif(strspn(url2,"
Apache"
)==6){cout"
Url2beginswiththe\"
Apache\"
//与strchr正好倒过来if(p=strrchr(emailaddr,'
b'
)){coutpendl;
}return0;
Ex3.倒置一个字符串
我们用了四种方法,最后一种方法经常在面试笔试题中出现。
2-3.cpp**Description:
Reverseastring**Version:
05/12/201003:
08:
09PM*Revision:
voidrevstr(char*str);
voidrevstr_p(char*str);
voidrevstr_recursive(char*str,intstart,intend);
char*(revstrcpy)(char*rstr,constchar*orgstr);
intmain(){charstr="
abcdefghijklmnopqrstuvwxyz"
char*des=newchar[27];
Originalstring:
strendl;
revstr(str);
Reversedstring:
revstr_p(str);
Reversedstringusingpointer:
revstr_recursive(str,0,strlen(str)-1);
Reversedstringusingrecursive:
Reversedstringusingcopymethod:
revstrcpy(des,str)endl;
voidrevstr(char*str){inti,j;
chart;
for(i=0,j=strlen(str)-1;
ij;
++i,--j){t=str[i];
str[i]=str[j];
str[j]=t;
}}
//Reverseastringinplace.Usepointersratherthanarrayindexing.voidrevstr_p(char*str){chart;
char*inc_p=str;
char*dec_p=&
str[strlen(str)-1];
while(inc_p=dec_p){t=*inc_p;
*inc_p++=*dec_p;
*dec_p--=t;
voidrevstr_recursive(char*str,intstart,intend){if(startend)revstr_recursive(str,start+1,end-1);
elsereturn;
chart=str[start];
str[start]=str[end];
str[end]=t;
char*(revstrcpy)(char*rstr,constchar*orgstr){//返回rstr的原始值使函数能够支持链式表达式,增加了函数的"
附加值"
。
同样功能的函数,如果能合理地提高的可用性,自然就更加理想。
if((rstr==NULL)||(orgstr==NULL))throw"
Invalidargument(s)"
char*dst=rstr;
dst+=strlen(orgstr);
*dst--='
while(*orgstr)*dst--=*orgstr++;
returnrstr;
Ex4.忽略大小写的字符串比较
2-4.cpp**Description:
IgnoretheLettercasewhencompared**Version:
44:
34PM*Revision:
intstrcmp_ign_case(constchar*str1,constchar*str2);
intmain(void){charstrA="
tesT"
charstrB="
Test"
charstrC="
testing"
charstrD="
Tea"
intresult;
//Comparestringsignoringcase.result=strcmp_ign_case(strA,strB);
result=strcmp_ign_case(strA,strC);
result=strcmp_ign_case(strA,strD);
result=strcmp_ign_case(strD,strA);
}//Asimplestringcomparisonfunctionthatignorescasedifferences.intstrcmp_ign_case(constchar*str1,constchar*str2){constchar*str1_cp=str1;
constchar*str2_cp=str2;
while(*str1_cp&
&
*str2_cp){if(tolower(*str1_cp++)!
=tolower(*str2_cp++))break;
}intresult=tolower(*str1_cp)-tolower(*str2_cp);
coutstr1"
is"
result)cout"
equalto"
elseif(result0)cout"
lessthan"
elsecout"
greaterthan"
coutstr2endl;
returnresult;
Ex5.搜索替换
2-5.cpp**Description:
Replacethesub_strwithanotherstr**Version:
05/12/201004:
07:
02PM*Revision:
#include#include#includeusingnamespacestd;
boolsearch_and_replace(char*orgstr,intmaxlen,constchar*oldsubstr,constchar*newsubstr);
char*search_and_replace_alloc(constchar*str,constchar*oldsubstr,constchar*newsubstr)throw(bad_alloc);
intmain(void){charstr[80]="
alphabetagammaalphabetagamma"
char*ptr=NULL;
str"
First,replaceallinstancesofalphawithepsilon.\n"
//Replacealloccurrencesofalphawithepsilon.while(search_and_replace(str,sizeof(str),"
alpha"
epsilon"
))cout"
Afterareplacement:
Second,replaceoneinstancesofepsilonwithalpha.\n"
try{ptr=search_and_replace_alloc(str,"
}catch(bad_allocexc){//Takeappropriateactionhere.}if(ptr){cout"
ptrendl;
deleteptr;
}boolsearch_and_replace(char*orgstr,intmaxlen,constchar*oldsubstr,constchar*newsubstr){char*pos=NULL;
if((orgstr==NULL)||(oldsubstr==NULL)||(newsubstr==NULL))returnfalse;
intlen=strlen(orgstr)-strlen(oldsubstr)+strlen(newsubstr);
if(lenmaxlen)returnfalse;
if(pos=strstr(orgstr,oldsubstr)){memmove(pos+strlen(newsubstr),pos+strlen(oldsubstr),strlen(pos)-strlen(oldsubstr)+1);
strncpy(pos,newsubstr,strlen(newsubstr));
returntrue;
}else{returnfalse;
char*search_and_replace_alloc(constchar*str,constchar*oldsubstr,constchar*newsubstr)throw(bad_alloc){constchar*pos=NULL;
if((str==NULL)||(oldsubstr==NULL)||(newsubstr==NULL))returnNULL;
intsize=strlen(str)+strlen(newsubstr)-strlen(oldsubstr)+1;
char*result=newchar[size];
if(pos=strstr(str,oldsubstr)){strncpy(result,str,pos-str);
*(result+(pos-str))='
strcat(result,newsubstr);
strcat(result,pos+strlen(oldsubstr));
coutreturnresult;
}else{deleteresult;
returnNULL;
Ex6.文本统计
2-6.cpp**Description:
TextstatisticsFunction**Version:
05/13/201002:
37:
structwc{//不使用class是因为这是一个纯数据的对象,虽然它包含一个默认的构造函数。
intwords;
intspaces;
intpunct;
intlines;
wc(){words=punct=spaces=lines=0;
}};
wcwordcount(constchar*str);
intmain(){constchar*test="
Bysupplyingastringclassandalso"
supportingnull-terminatedstrings,\nC++"
offersarichprogrammingenvironmentfor"
string-intensivetasks.\nIt'
spowerprogramming."
Given:
couttestendl;
wcwcd=wordcount(test);
\nWords:
wcd.wordsendl;
Spaces:
wcd.spacesendl;
Lines:
wcd.linesendl;
Punctuation:
wcd.punctendl;
}wcwordcount(constchar*str){wcdata;
if(*str)++data.lines;
while(*str){if(isalpha(*str)){while(isalpha(*str)||*str=='
\'
'
){if(*str=='
)++data.punct;
++str;
}data.words++;
}else{if(ispunct(*str))++data.punct;
elseif(isspace(*str)){++data.spaces;
if(*str=='
\n'
*(str+1))++data.lines;
}++str;
}}returndata;
Ex7解析一个C型字符串
2-7.cpp**Description:
StringToken**Version:
05/14/201010:
01:
58AM*Revision:
#defineMAX_TOKEN_SIZE128constchar*gettoken(constchar*str);
intmain(){chardelims="
.,?
!
charstr="
Ilikeapples,pears,andgrapes.Doyou?
char*tok;
Obtainthewordsinasentence.\n"
tok=strtok(str,delims);
while(tok){couttokendl;
tok=strtok(NULL,delims);
}charkvpairs="
count=10,name=\"
TomJones,jr.\"
max=100,min=0.01"
charkvdelims="
=,"
\nTokenizekey/Valuepairs.\n"
tok=strtok(kvpairs,kvdelims);
while(tok){cout"
Key:
tok"
strcmp("
name"
tok)){tok=strtok(NULL,"
\"
}else{tok=strtok(NULL,kvdelims);
}cout"
Value:
tokendl;
tok=strtok(NULL,kvdelims);
OriStringis"
kvpairs
//Wewanttotokenthecount,12andthesymboladd(+),butwecannotmakeitviastrtokcharcount="
max=12+3/89;
count27=19*(min+floor);
charcountdelims="
=+"
constchar*strtok=gettoken(count);
while(strtok){coutstrtokendl;
strtok=gettoken(NULL);
constchar*gettoken(constchar*str){staticchartoken[MAX_TOKEN_SIZE+1];
//staticmakesthereturnmethodcanbemade.staticconstchar*ptr;
//statictypeholdsthestringlasttimepassedinintcount=0;
//holdsthecurrentcharactercountchar*tokptr=token;
if(str){ptr=str;
}while(isspace(*ptr))ptr++;
if(isalpha(*ptr)){while(isalpha(*ptr)||isdigit(*ptr)){*tokptr++=*ptr++;
++count;
if(count==MAX_TOKEN_SIZE)break;
}}elseif(isdigit(*ptr)){while(isdigit(*ptr)){*tokptr++=*ptr++;
}}elseif(ispunct(*ptr)){*tokptr++=*ptr++;
}elsereturnNULL;
//Nullterminatethetoken.*tokptr='
returntoken;
3.String操作实例:
Ex8String基本操作
2-8.cpp**Description:
StringBasicOperation**Version:
05/14/201002:
15:
06PM*Revision:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 用思维导图和实例学习C 之一 字符串处理 思维 实例 学习 字符串 处理
![提示](https://static.bdocx.com/images/bang_tan.gif)