首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 计算文本中的行数,字符串数以及字符个数
【标  题】:计算文本中的行数,字符串数以及字符个数
【关键字】:
【来  源】:http://blog.csdn.net/ningboweimin/archive/2006/11/08/1373872.aspx

计算文本中的行数,字符串数以及字符个数

计算文本中的行数,字符串数以及字符个数

#include <stdio.h>

#include <string.h>

main()

{

char line[100],ctr;
 int i,c,
  end = 0,
  characters = 0,
  words = 0,
  lines = 0;

 printf("Key in the text.\nGive one space after each word.\n");
 printf("When completed,press 'Return'.^_^\n\n");

 while (end == 0)
 {
  //reading a line of text.
  c = 0;
  while((ctr=getchar())!='\n')
   line[c++]=ctr;
  line[c]='\0';
  //counting the words in a line.
  if (line[0]=='\0')
  {
   break;
  }
  else
  {
   words++;
   for(i=0;line[i]!='\0';i++)
    if(line[i] == ' '|| line[i] == '\t')
     words++;
    

  }
     //counting lines and characters.
  lines++;
  characters = characters + strlen(line);
 }

 printf("\nNumber of lines = %d\n",lines);
 printf("Number of words = %d\n",words);
 printf("Number of characters = %d\n",characters);

同步转为异步的OO解决之道:【上一篇】
开篇之作 关于c++中多个基类有相同的方法的解决办法:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • Maven入门--较复杂的实例
  • C程序员的境界
  • 富士通携手博科试水虚拟化业务
  • 广州地铁局域网和城域网解决方案(7)
  • 为工具栏创建下拉式按扭
  • 编辑尺寸标注
  • Proxool 与 Spring 真的水土不服么?
  • MySQL 4.1.4 关于中文的问题
  • 关于23种设计模式的有趣见解(转)
  • 工作中八大最易被淘汰的人[转]
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.