以下是C語言代碼:(請參看注釋)
#include
#include
#include
usingnamespacestd;
intmain(intargc,char*argv[])
{
intcount=0;
char*str ;
printf("Inputastring:");
gets(str);//此處不能使用scanf(%s,str)或者cin>>str; 因?yàn)檫@兩者個函數(shù)在執(zhí)行過程中發(fā)現(xiàn)字符串中還有空格
//或者回車符就會結(jié)束運(yùn)行。故無法通過這兩個函數(shù)計算字符串中的字符數(shù)
char*p=str;
while(*p!=’’)
{
if(*p==’’)count++;
p++;
}
cout<<"Yourinputstringis:"< cout<<"TheCountofspace="< system("PAUSE");
return0;
}
#include
#include
#include
usingnamespacestd;
intmain(intargc,char*argv[])
{
intcount=0;
char*str ;
printf("Inputastring:");
gets(str);//此處不能使用scanf(%s,str)或者cin>>str; 因?yàn)檫@兩者個函數(shù)在執(zhí)行過程中發(fā)現(xiàn)字符串中還有空格
//或者回車符就會結(jié)束運(yùn)行。故無法通過這兩個函數(shù)計算字符串中的字符數(shù)
char*p=str;
while(*p!=’’)
{
if(*p==’’)count++;
p++;
}
cout<<"Yourinputstringis:"<
return0;
}