#include <iostream>
#include <string>using namespace std;int cinstr(const char* str,char ch){
int count=0;
while(*str)//为什么用str的值来作循环的条件呢?是什么意思呢??
{
if(*str==ch){
count++;
}
str++;
}
return count;
}
int main(){ char* c="this isx xpar";
int count=cinstr(c,'x');
cout<<count<<endl;
system("pause");
return 0;}
可是我还是不能理解while(*str)是如何遍历到结束的,即为0的???????
这句话就是使指针向后移一个字符,(str代表字符的地址,*str就是这个地址里的字符值了啦)。
然后比较每个字符,值到字符串结尾出现\0字符的时候,*str的值就是0了,就退出循环了。