读取字符串数目

用tag标记

当 *buff!=' '且tag=1时候 tag=0 num++;

*buff='  '  tag=0时候tag=1 

所以初始化tag=1

代码:

 标准输入     fgets(buff,1000,stdin)

#include<iostream>
using namespace std;
int gcd(char *buff)
{
    char *p=buff;
    int tag=1,num=0;
    //0 10 13 表示 空字符 换行键 回车符 
    for(;*p!=0&&*p!=10&&*p!=13;p++){
    if(*p==' '&&!tag) tag=1;
    else if(*p!=' '&&tag)    {
        num++;
        tag=0;
    }
    }
    return num;
}
int main()
{
    char buff[1000];
    fgets(buff,1000,stdin);
    cout<<buff<<endl;
    cout<<gcd(buff);
}
View Code
原文地址:https://www.cnblogs.com/helloworld2019/p/10382565.html