运行后可能出现警告,但是不影响结果,出现警告的原因是 s存放在constan区域,但在程序的声明中却用指针进行声明的。
#include<iostream>
#include<cstring>
using namespace std;
class wordnum
{
char str[80];
int c;
public:
wordnum(char *s)
{
strcpy(str,s);
c=0;
}
void process();
void print()
{
cout<<str<<endl;
cout<<"num="<<c<<endl;
}
};
void wordnum::process()
{
int word=1;
int len;
len=strlen(str);
for(int i=0;i<len;i++)
{
if(((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))&&word)
{
c++;
word=0;
}
else if(str[i]==' ')
word=1;
}
}
int main()
{
wordnum w("she is a nice girl");
w.process();
w.print();
return 0;
}