标签:字符串
#pragma once #include<iostream.h> #include<assert.h> #include<string.h> #define MAX_STRING_SIZE 20 typedef char SString[MAX_STRING_SIZE+1]; void Show(); //菜单显示 void StrAssign(SString T, char *str);//初始化字符串 void PrintString(char T[]); //显示字符串 int Length(SString s); //求字符串长度 bool StrEmpty(SString S);//判空 void StrCopy(SString T, SString S);//复制字符串 void DestroyString(SString S); //摧毁字符串 void StrDelete(SString T, int pos, int len); //按位置和长度删除字符串 void Replace(SString T, SString P, SString V,int pos);//按位置替换字符串 int Index(SString T, int pos, SString P);//按位置匹配字符串 void StrInsert(SString D, int pos, SString T);//按位置插入字符串 void SubString(SString D, SString S, int pos, int len);//按位置获取字符串 void ClearString(SString S); //清理字符串 int StrCompare(SString T, SString S);//比较字符串 void StringConcat(SString T, SString s1, SString s2);//链接字符串2:功能模块Sting.cpp
#include"String.h" void Show() { cout<<"*********************************************"<<endl; cout<<"*[1]: 初始化字符串 [2]:拷贝字符串 *"<<endl; cout<<"*[3]: 比较字符串 [4]:求字符串长度*"<<endl; cout<<"*[5]: 显示字符串 [6]:清理字符串 *"<<endl; cout<<"*[7]: 链接字符串 [8]:获取子串 *"<<endl; cout<<"*[9]: 匹配字符串 [10]:替换字符串 *"<<endl; cout<<"*[11]: 插入字符串 [12]:按位删除 *"<<endl; cout<<"*[13]: 摧毁字符串 [0]:结束程序 *"<<endl; cout<<"*********************************************"<<endl; cout<<"请选择:"; } int Length(SString s) { return strlen(s); } void StringConcat(SString T, SString s1, SString s2) { for(int i=0;i<Length(s1);i++) { T[i]=s1[i]; } for(int j=0;j<Length(s2);j++) { T[i+j]=s2[j]; } T[i+j]='\0'; } void PrintString(char T[]) { cout<<T<<endl; } void StrAssign(SString T, char *str) { for(int i=0;i<Length(T);i++) { T[i]=str[i]; } T[i]='\0'; } void StrCopy(SString T, SString S) { for(int i=0;i<=Length(T);i++) { T[i]=S[i]; } } bool StrEmpty(SString S) { return S[0]=='\0'; } int StrCompare(SString T, SString S) { int i=0; while(T[i]!='\0' || S[i]!='\0') { if(T[i]> S[i]) return 1; if(T[i]< S[i]) return -1; else i++; } return 0; } void ClearString(SString S) { S[0]='\0'; } void SubString(SString D, SString S, int pos, int len) { if(pos<0 || pos>Length(S)) return; if(len>Length(S)-pos+1) return; for(int i=0;i<len;i++) { D[i]=S[i+pos]; } D[i]='\0'; } void StrInsert(SString D, int pos, SString T) { int t_length=Length(T); int d_length=Length(D); for(int i=d_length;i>=pos;i--) { D[i+t_length]=D[i]; } for(i=0;i<t_length;i++) { D[pos+i]=T[i]; } } int Index(SString T, int pos, SString P) { int t_length=Length(T); int p_length=Length(P); for(int i=pos;i<t_length-p_length;i++) { for(int j=0;j<p_length;j++) { if(T[i+j]==P[j]) continue; else break; } if(j==p_length) return i; } return -1; } void Replace(SString T, SString P, SString V,int pos) { int ret=Index(T,pos,P); if(-1==ret) { cout<<"没有找到需要替换的位置,程序结束"<<endl; return; } int v_length=Length(V); for(int i=0;i<v_length;i++) { T[i+ret]=V[i]; } } void StrDelete(SString T, int pos, int len) { if(pos<0 || pos>=Length(T)) return; if(pos+len>Length(T)) return; for(int i=0;i<=Length(T)-pos-len;i++) { T[i+pos]=T[i+len+pos]; } } void DestroyString(SString S) { S[0] = '\0'; }3:主函数main.cpp
#include"String.h" void main() { char *str = "ABCDEFG"; char *str1 ="EF"; char *str2="ab"; SString RET; SString T; SString P; SString S; int length; int pos=1; while(pos) { Show(); cin>>pos; switch(pos) { case 1: StrAssign(S,str); StrAssign(T,str1); StrAssign(P,str2); break; case 2: StrCopy(RET,T); cout<<"拷贝后字符串:"; PrintString(RET); break; case 3: cout<<"**相等为:0,大于为:1小于为:-1**"<<endl; cout<<"比较后的结果:"<<StrCompare(S,T)<<endl; break; case 4: cout<<"字符串长度:"<<Length(S)<<endl; break; case 5: PrintString(S); PrintString(T); PrintString(P); break; case 6: ClearString(S); break; case 7: StringConcat(RET,S,T); cout<<"链接后的字符串:"; PrintString(RET); break; case 8: cout<<"输入需要获取子串位置的下标和长度:"<<endl; cin>>pos; cin>>length; SubString(RET,S,pos,length); cout<<"子串:"; PrintString(RET); break; case 9: pos=Index(S,0,T); if(-1==pos) { cout<<"匹配不成功"<<endl; } else cout<<"在_"<<pos<<"_匹配成功"<<endl; break; case 10: Replace(S,T,P,0); PrintString(S); break; case 11: cout<<"输入需要插入位置的下标:"<<endl; cin>>pos; StrInsert(S,pos,P); cout<<"插入后的字符串:"; PrintString(S); break; case 12: cout<<"输入需要删除位置的下标和个数:"<<endl; cin>>pos; cin>>length; StrDelete(S,pos,length); cout<<"删除后的字符串:"; PrintString(S); break; case 13: DestroyString(S); break; default : break; } } }
标签:字符串
原文地址:http://blog.csdn.net/zhou753099943/article/details/45896975