zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/xkfz007/p/2655852.html
Copyright © 2011-2022 走看看