以下是C语言代码:(请参看注释)
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
usingnamespacestd;
intmain(intargc,char*argv[])
{
intcount=0;
char*str ;
printf("Inputastring:");
gets(str);//此处不能使用scanf(%s,str)或者cin>>str; 因为这两者个函数在执行过程中发现字符串中还有空格
//或者回车符就会结束运行。故无法通过这两个函数计算字符串中的字符数
char*p=str;
while(*p!=’’)
{
if(*p==’’)count++;
p++;
}
cout<<"Yourinputstringis:"<<str<<endl;
cout<<"TheCountofspace="<<count<<endl;
system("PAUSE");
return0;
}
责任编辑:小草