C++与C中getwords函数的实现

getword函数
这儿所说的getword函数,是指从一段字符串中提取出其中的单词。其实这儿主要是分析利用一些标点符号和一些空白字符进行分割而成的单词,还没有达到编译器所使用的对字符串的分析。编译器是根据语言的文法对语言的源代码进行分析。一般需要利用编译原理中的文法分析来得到具体的token。而这儿的比较简单。
对于C和C++有不同的实现方式。
在C语言中有一个函数,strtok可以帮助实现这个。这个函数主要是根据给定的分隔符来对字符串进行分割。
实现代码如下:
/*
 * 10. C语言中利用strtok对字符串根据特定的字符进行划分
 
*/
#include <stdio.h>
#include < string.h>
#include <stdlib.h>
int getwords( char* words[], char *line, const  char* delim){
         int count;
         char* token;
        count= 0;
        token=strtok(line,delim);
        words[count++]=token;
         while((token=strtok(NULL,delim))!=NULL)
            words[count++]=token;
         return count;
}
int main()
{
     char *path= " /etc/passwd ";
     char buf[BUFSIZ];
     char *words[ 100];
     char *delim= " :/ ";
    FILE* fp=fopen(path, " r ");
     if(!fp)
    {
        printf( " Can't open file:%s\n ",path);
        exit(- 1);
    }
     while(fgets(buf,BUFSIZ,fp))
    {
        printf( " %s ",buf);
         int count=getwords(words,buf,delim);
         int i;
         if(count> 0)
         for(i= 0;i<count;i++)
            printf( " ---->%s\n ",words[i]);
    }
    fclose(fp);
}

在C++中可以利用string类的一些函数来实现,包括find_first_of和find_first_not_of
代码如下:
/*
 * 6. 对文本进行简单的单词划分
 * 
*/
#include <iostream>
#include < string>
#include <vector>
using  namespace std;
void getwords(vector< string>& words, string line, string sep= "  :;,.\t\v\r\n\f[]{}()<>+-=*&^%$#@!~`\'\"|\\/? "){
     string word;
     string::size_type pos= 0,pos2= 0;                              
     while((pos2=line.find_first_not_of(sep,pos))
            != string::npos){
        pos=line.find_first_of(sep,pos2);
         if(pos!= string::npos){
            word=line.substr(pos2,pos-pos2);
        }
         else{
            word=line.substr(pos2);
        }
        words.push_back(word);
    }
}
int main(){
     string line;
     string sep( "  :;,.\t\v\r\n\f[]{}()<>+-=*&^%$#@!~`\'\"|\\/? ");
    vector< string> words;
     while(getline(cin,line)){
        getwords(words,line,sep);
    }
    vector< string>::iterator ite;
     for(ite=words.begin();ite!=words.end();++ite)
        cout<<*ite<<endl;
}

 

其实在C语言中还有一个函数strpbrk,这个函数在串中查找给定字符集中的字符所在的位置。 也可以实现获取单词的功能。

对应的C代码如下:

#include <stdio.h>
#include < string.h>
int getwords( char* words[], char* line, const  char* delim){
     char *pos,*pos2;
     int count= 0;
     int k= 0;
    pos2=line;
     while((pos=strpbrk(pos2,delim))!=NULL){
        *pos= ' \0 ';
         if(pos2!=pos)
            words[count++]=pos2;
        pos2=pos+ 1;
    }
     if(*pos2!= ' \0 ')
        words[count++]=pos2;
     return count;
}
int main(){
     char line[BUFSIZ];
     char *delim= "  :;,.\t\v\r\n\f[]{}()<>+-=*&^%$#@!~`\'\"|\\/? ";
     char* words[ 100];
     int count;
     while(fgets(line,BUFSIZ,stdin)){
        printf( " %s ",line);
        count=getwords(words,line,delim);
         if(count> 0){
             int i;
             for(i= 0;i<count;i++)
                printf( " ----->%s\n ",words[i]);
        }
    }
}

 

strpbrk函数另一个用处,查找字符:

#include <stdio.h>
#include < string.h>
int getchars( int *chars, const  char *line, const  char *key){
     int count= 0;
     const  char*pch;
    pch=line;
     while((pch=strpbrk(pch,key))!=NULL){
        chars[count++]=pch-line;
        pch++;
    }
     return count;
}
int main(){
     char line[BUFSIZ];
     char *key =  " aeiou ";
     int chars[BUFSIZ];
     int count;
     while(fgets(line,BUFSIZ,stdin)){
        printf( " %s ",line);
        count=getchars(chars,line,key);
         if(count> 0){
             int i;
             for(i= 0;i<count;i++)
                printf( " ----->%d %c\n ",chars[i],line[chars[i]]);
        }
    }
    
     return  0;
}

 


转载于:https://www.cnblogs.com/xkfz007/archive/2012/08/25/2655852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值