利用指针进行字符串的统计、复制、删除
代码如下:
#include <iostream> #include <algorithm> #include <string> using namespace std; //指针练习 //字符串字数统计 int mystr1(char *x) { int c=0; while(*x!='\0') { c++; x++; } return c; } //字符串复制 mystr2(char *s,char *d) { while(*s!='\0') { *d=*s; s++; d++; } } //删除指定字符 mystr3(char *x,char y) { while(*x!='\0') { if(*x==y) { for(char *t=x;*t!='\0';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; }