| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 491 人关注过本帖
标题:字元检查请教
只看楼主 加入收藏
牛bkk
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-5-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
字元检查请教
想编写一个从char*检查特定字元, 但无从入手, 请各大神指教一下

例: 检查字元是 'is'

int countResult(char source[])
{

}

int main()
{
char* source = "This is an apple";
int count = 0;
count = countResult(source);

printf("Result: %d", count)//Output: Result: 2
}
搜索更多相关主题的帖子: source apple count 
2014-05-01 22:00
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
这个有现成的库函数  你是要练习还是什么

DO IT YOURSELF !
2014-05-01 22:02
牛bkk
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-5-1
收藏
得分:0 
是用来练习的, 因为初学C语言, 从书中看到这些, 但不太懂
2014-05-01 22:05
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
逐一比较  貌似也没啥好招

DO IT YOURSELF !
2014-05-01 22:20
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
無從入手?你自己完全沒有思路?先別想程序怎麽寫,先想手工做的話,是怎樣做。

授人以渔,不授人以鱼。
2014-05-01 22:23
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
程序代码:
#include <stdio.h>

typedef int BOOL;
#define FALSE    0
#define TRUE    1

BOOL is_space(const char ch);
BOOL string_compare(const char* source, const char* target);
int countResult(const char* source, const char* target);

int main(void)
{
    const char* source = "This is an apple";
    const char* target = "is";
    printf_s("Source string: %s\n", source);
    printf_s("Target string: %s\n", target);
    printf_s("Result: %d\n", countResult(source, target));

    getchar();
    return 0;
}

BOOL is_space(const char ch)
{
    return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n');
}

BOOL string_compare(const char* source, const char* target)
{
    BOOL is_equivalent = TRUE;        // 空串與任何串相等

    while (*target != '\0')
    {
        if (*source++ != *target++)
        {
            is_equivalent = FALSE;
            break;
        }
    }

    return is_equivalent;
}

int countResult(const char* source, const char* target)
{
    int count = 0;
    BOOL in_word = FALSE;

    if (*source == '\0')
    {
        return count;
    }

    while (*source != '\0')
    {
        if (!is_space(*source) && !in_word)
        {
            in_word = TRUE;
            if (string_compare(source, target))
            {
                break;
            }
        }
        if (is_space(*source) && in_word)
        {
            ++count;
            in_word = FALSE;
        }
        ++source;
    }

    return count + 1;
}



[ 本帖最后由 TonyDeng 于 2014-5-2 01:48 编辑 ]

授人以渔,不授人以鱼。
2014-05-02 01:41
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
注意:不能用strstr()函數,必須避免誤判This中的is,那不是所求。

樓上的代碼,除了printf()之外,沒有用任何庫函數。若要用庫函數,必須提取單詞再比較,那個庫函數對初學者不是那麽好掌握的。

[ 本帖最后由 TonyDeng 于 2014-5-2 01:55 编辑 ]

授人以渔,不授人以鱼。
2014-05-02 01:53
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
修复了一個bug並作些小修改:

程序代码:
#include <stdio.h>

typedef int BOOL;
#define FALSE    0
#define TRUE    !0

BOOL is_space(const char ch);
BOOL string_compare(const char* source, const char* target);
int countResult(const char* source, const char* target);

int main(void)
{
    const char* source = "This is an apple";
    const char* target = "is";
    printf_s("Source string: %s\n", source);
    printf_s("Target string: %s\n", target);
    printf_s("Result: %d\n", countResult(source, target));

    getchar();
    return 0;
}

BOOL is_space(const char ch)
{
    return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n') ? TRUE : FALSE;
}

BOOL string_compare(const char* source, const char* target)
{
    BOOL is_equivalent = TRUE;        // 空串與任何串相等

    while (*target != '\0')
    {
        if (*source++ != *target++)
        {
            is_equivalent = FALSE;
            break;
        }
    }

    return is_equivalent;
}

int countResult(const char* source, const char* target)
{
    int count = 0;
    BOOL in_word = FALSE;
    BOOL found = FALSE;

    while (*source != '\0')
    {
        if (!is_space(*source) && !in_word)
        {
            in_word = TRUE;
            if (string_compare(source, target))
            {
                found = TRUE;
                break;
            }
        }
        if (is_space(*source) && in_word)
        {
            ++count;
            in_word = FALSE;
        }
        ++source;
    }

    return found ? count + 1 : 0;
}



[ 本帖最后由 TonyDeng 于 2014-5-2 22:04 编辑 ]

授人以渔,不授人以鱼。
2014-05-02 21:54
快速回复:字元检查请教
数据加载中...
 
   



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

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