Welcome

首页 / 软件开发 / C语言 / C语言中关于计算字符串中空格数的问题

C语言中关于计算字符串中空格数的问题2010-06-17以下是代码:(请参看注释)

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
int count = 0 ;
char* str;
printf("Input a string:");
gets(str); //此处不能使用scanf(%s,str)或者cin>>str; 因为这两者个函数在执行过程中发现字符串中还有空格
//或者回车符就会结束运行。故无法通过这两个函数计算字符串中的字符数
char* p = str ;
while(*p!="")
{
if(*p==" ") count++ ;
p++ ;
}
cout<<"Your input string is :"<<str<<endl ;
cout<<"The Count of space= "<<count<<endl ;
system("PAUSE");
return 0;
}