| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 9922 人关注过本帖, 1 人收藏
标题:在字符串中提取单词的方法
只看楼主 加入收藏
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
顺带回应“无功”网友询问在字符串中提取数字的办法:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void Pause(const char* message);
bool ScanInteger(const char* str, int* value);

int main(void)
{
    const char* str = "asd123qwe45 6mnb789xyz";
    int value;

    while (ScanInteger(str, &value))
    {
        printf_s("%d\n", value);
        str = NULL;
    }

    Pause(NULL);
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    if (message == NULL)
    {
        message = "\nPress <Enter> key to continue...";
    }
    fflush(stdin);
    getchar();
}

bool ScanInteger(const char* str, int* value)
{
    char buffer[20];
    static const char* ptrStr = NULL;
    char* ptrBuffer = buffer;
    bool begin = false;
    bool end = false;

    if (str != NULL)
    {
        ptrStr = str;
    }
    while ((*ptrStr++ != '\0') && !end)
    {
        if (isdigit(*ptrStr))
        {
            if (!begin)
            {
                begin = true;
            }
            *ptrBuffer++ = *ptrStr;
        }
        else
        {
            if (begin)
            {
                end = true;
                *ptrBuffer = '\0';
                *value = atoi(buffer);
            }
        }
    }
    --ptrStr;

    return end;
}



[ 本帖最后由 TonyDeng 于 2015-4-15 20:24 编辑 ]

授人以渔,不授人以鱼。
2015-04-15 20:18
SunshineGirl
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:129
专家分:131
注 册:2012-3-20
收藏
得分:0 
每天回帖即可获得10分可用分
2015-04-16 12:27
抠脚菜鸟
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2016-3-20
收藏
得分:0 
如果是提取字符串中的汉字的话该怎么呢?
2016-05-08 14:49
快速回复:在字符串中提取单词的方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026930 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved