关于计算字符串中空格数的问题
来源:优易学  2011-12-13 13:23:24   【优易学:中国教育考试门户网】   资料下载   IT书店
  以下是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;
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训