您的位置:首页 > 编程语言 > C语言/C++

C语言:关于计算字符串中空格数的问题

2008-11-22 22:16 513 查看
C语言:关于计算字符串中空格数的问题

以下是C语言代码:(请参看注释)

#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!='/0')

{

if(*p==' ') count++ ;

p++ ;

}

cout<<"Your input string is :"<<str<<endl ;

cout<<"The Count of space= "<<count<<endl ;

system("PAUSE");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: