指针练习-字符串函数

利用指针进行字符串的统计、复制、删除

代码如下:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
//指针练习 
//字符串字数统计
int mystr1(char *x) 
{
    int c=0;
    while(*x!='')
    {
        c++;
        x++;
    }
    return c;
}
//字符串复制
mystr2(char *s,char *d) 
{
    while(*s!='')
    {
        *d=*s;
        s++;
        d++;
    }
}
//删除指定字符 
mystr3(char *x,char y) 
{
    while(*x!='')
    {
        if(*x==y)
        {
            for(char *t=x;*t!='';t++)
            {
                *t=*(t+1);
            }
        }
        x++;
    }
}
main()
{
    int n;
    char a[]="He is a king who loved horses.",b[40];
    n=mystr1(a);
    cout<<n<<endl;
    mystr2(a,b);
    cout<<b<<endl;
    mystr3(a,'o');
    cout<<a;
}
原文地址:https://www.cnblogs.com/wanjinliu/p/11409681.html