首页 > 技术文章 > 基于C语言的Wordcount

qiyi001 2017-09-27 11:27 原文

     该程序引用了LNZ001的博客笔记,链接地址:http://blog.csdn.net/LNZ001/article/details/54851551。

   Github地址:https://github.com/Divel-Qin/C-Program/tree/master/Wordcount

由于自己基础比较薄弱,所以就引用了网上的程序。程序包括字符处理,单词处理,文本处理。程序大概能看懂,程序中用到了指针,虽然自己对指针也不太熟悉,但还是能大概了解。主要代码如下:

提取单词:

  1. int index = 0;  
  2.     while(true){  
  3.         while(text[index] == space)  
  4.             ++index;  
  5.         if(text[index] == '\0')  
  6.             break;  
  7.         wordlen = 0;  
  8.         while(text[index] == quote || isalnum(text[index])){  
  9.             if(wordlen == WORDLEN){  
  10.                 printf("超出单个单词最大长度.(%d)",WORDLEN);  
  11.                 return 1;  
  12.             }  
  13.             word[wordlen++] = tolower(text[index++]);  
  14.         }  
  15.         word[wordlen] = '\0';  

替换字母,数字以外的所有符号为空格:

  1. for(int i = 0; i < strlen(text); i++){  
  2.         if(text[i] == quote || isalnum(text[i])){  
  3.             continue;  
  4.         }  
  5.         text[i] = space;  
  6.     }  

 

推荐阅读