自己写的字符串函数

#include <iostream>

using namespace std;

#define N 50

//将字符串src复制到desti
void myStrcpy(char *desti,char *src)
{
 while (*src)
 {
  *(desti++) = *(src++);
 }
 *desti = '\0';
}

//比较字符串大小
int myStrcmp(char *str1,char *str2)
{
 char *p1,*p2;
 p1 = str1;
 p2 = str2;
 while(*p1 == *p2)  
 {
  if(*p1=='\0' && *p2=='\0')
   break;
  p1++;
  p2++;
 }
 return *p1 - *p2;
}

//将后一个字符串拼接到前一个字符串
void myStrcat(char *p1,char *p2)
{
 while(*p1) 
  p1++;
 while (*p2)
 {
  *p1++ = *p2++;
 }
 *p1 = '\0';
}

//测试字符串长度
int myStrlen(char *p)
{
 int cnt=0;
 while (*p++)
 {
  cnt++;
 }
 return cnt;
}

//得到字符串第n个字符串
char myStrch(char *p,int n)
{ 
 if (n>myStrlen(p))
 {
  cout<<"输入数字大于字符串长度"<<endl;
  return 0;
 }
 for (int i=1;i<n;i++,p++)
  ;
 return *p;
}

//交换两个字符串
void myStrswap(char *p1,char*p2)
{
 char *pt1,*pt2,tmp;
 pt1 = p1;
 pt2 = p2;
 while (*pt1 !='\0' || *pt2!='\0')
 {
  tmp = *pt1;
  *pt1 = *pt2;
  *pt2 = tmp;
  pt1++;
  pt2++;
 }
}

//将字符串逆置
void myStrnz(char *p)
{
 char tmp;
 char *p1,*p2;
 p1=p;
 while(*p1) p1++;
 p1--;
 for (p2=p;p2<p1;p1--,p2++)
 {
  tmp = *p1;
  *p1 = *p2;
  *p2 = tmp;
 }
}

//将字符串各字符按ASCII码升序排列
void myStrsort(char *p)
{
 char tmp;
 char *p1,*p2;
 for (p1=p;*(p1+1);p1++)
 {
  for (p2=p1+1;*p2;p2++)
  {
   if (*p1>*p2)
   {
    tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
   }
  }
 }
}

//将字符串加密,加密函数为 ch = (ch+3)%7
void myStrpwd(char *str)
{
 char *p =str;
 while (*p)
 {
  *p = (*p)*11%256;
  p++;
 }
}

//得到字符串从第begin到end个字符的字串
void myStrsub(char *substr,char *str,int begin,int end)
{
 char *p;
 int i;
 if (begin>end || (end-begin)>(myStrlen(str)-begin))
 {
  cout<<"输入区间错误"<<endl;
  return ;
 }
 for(i=1,p=str;i<begin;i++,p++) 
  ;
 for ( ;i<=end;i++,p++)
 {
  *substr++ = *p;
 }
 *substr = '\0';
}

int main()
{
 int index;
 while (1)
 {
  system("cls");
  cout<<"\t=====字符串函数集功能演示=====\n"
   <<"\t\t1.字符串复制\n"
   <<"\t\t2.字符串比较大小\n"
   <<"\t\t3.字符串拼接\n"
   <<"\t\t4.测试字符串长度\n"
   <<"\t\t5.得到字符串特定字符\n"
   <<"\t\t6.交换字符串\n"
   <<"\t\t7.字符串逆置\n"
   <<"\t\t8.字符串按字符升序排序\n"
   <<"\t\t9.字符串加密\n"
   <<"\t\t0.获取子字符串"
   <<endl;
  cout<<"请输入序号,进入相应功能:";
  cin>>index;
  switch (index)
  {
  case 1:
   {
    char str1[N]="\0",str2[N]="\0";
    cout<<"请输入你要复制的字符串:"<<endl;
    cin>>str2;
    myStrcpy(str1,str2);
    cout<<"复制后的字符串为:"<<str1<<endl;
    system("pause");
   }
   break;
  case 2:
   {
    char p1[N]="\0",p2[N]="\0";
    cout<<"请输入你要比较的两个字符串:"<<endl;
    cin>>p1>>p2;
    if (myStrcmp(p1,p2)>0)
    {
     cout<<p1<<"大于"<<p2<<endl;
    } 
    else if(myStrcmp(p1,p2) == 0)
    {
     cout<<p1<<"等于"<<p2<<endl;
    }
    else
     cout<<p1<<"小于"<<p2<<endl;  
    system("pause");
   }
   break;
  case 3:
   {
    char p1[N]="\0",p2[N]="\0";
    cout<<"请输入两个字符串,前一个为被拼接的字符串,本程序将把后一个拼接在前一个之后"<<endl; 
    cin>>p1>>p2;
    myStrcat(p1,p2);
    cout<<"拼接后的字符串为:"<<p1<<endl;
    system("pause");
   }
   break;
  case 4:
   {
    int cnt = 0;
    char p[N]="\0";
    cout<<"请输入你要测试长度的字符串"<<endl;
    cin>>p;
    cnt = myStrlen(p);
    cout<<"字符串长度为:"<<cnt<<endl;
    system("pause");
   }
   break;
  case 5:
   {
    int i;
    char ch,p[N]="\0";
    cout<<"请输入字符串:"<<endl;
    cin>>p;
    cout<<"请输入你想得到的字符的序号:"<<endl;
    cin>>i;
    ch = myStrch(p,i);
    cout<<"第"<<i<<"个字符为:"<<ch<<endl;
    system("pause");
   }
   break;
  case 6:
   {
    char p1[N]="\0",p2[N]="\0";
    cout<<"请输入两个字符串:" <<endl;
    cin>>p1>>p2;
    myStrswap(p1,p2);
    cout<<"交换后的字符串为:\n"<<p1<<"\t"<<p2<<endl;
    system("pause");
   }
   break;
  case 7:
   {
    char p[N]="\0";
    cout<<"请入你要逆置的字符串"<<endl;
    cin>>p;
    myStrnz(p);
    cout<<"逆置后的字符串为:"<<p<<endl;
    system("pause");
   }
   break;
  case 8:
   {
    char p[N]="\0";
    cout<<"请输入要排序的字符串:"<<endl;
    cin>>p;
    myStrsort(p);
    cout<<"排序后的字符串为:"<<p<<endl;
    system("pause");
   }
   break;
  case 9 :
   {
    char p[N]="\0";
    cout<<"请输入要加密的字符串:"<<endl;
    cin>>p;
    myStrpwd(p);
    cout<<"加密后的字符串为:"<<p<<endl;
    system("pause");
   }
   break;
  case 0:
   {
    char p1[N]="\0",p2[N]="\0";
    int i,n;
    cout<<"请输入字符串及子字符串的起始和终止位置(如 abcdef 2 3):"<<endl;
    cin>>p1>>i>>n;
    myStrsub(p2,p1,i,n);
    cout<<"得到的子字符串为:"<<p2<<endl;
    system("pause");
   }
   break;
  default:
   {
    cout<<"输入有误,请输入0-9的数字:"<<endl;
    system("pause");
   }
   break;
  }
 }

 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值