计数中℃的字符串的字符出现的次数 [英] Counting the number of times a character occurs in a string in C

查看:113
本文介绍了计数中℃的字符串的字符出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C,和我的工作对我自己的爆炸类似的功能。我想算一笔指定字符在字符串中出现的次数。

I'm new to C, and I'm working on my own explode like function. I'm trying to count how many times a specified character occurs in a string.

int count_chars(char * string, char * chr)
{
    int count = 0;
    int i;

    for (i = 0; i < sizeof(string); i++)
    {
        if (string[i] == chr)
        {
            count++;
        }
    }

    return count;
}

这只是返回0每次。任何人都可以解释为什么吗? :)

It just returns 0 every time. Can anyone explain why, please? :)

推荐答案

您code是无可救药的缺陷。下面是它的的样子:

Your code is hopelessly flawed. Here's how it should look like:

int count_chars(const char* string, char ch)
{
    int count = 0;
    int i;

    // We are computing the length once at this point
    // because it is a relatively lengthy operation,
    // and we don't want to have to compute it anew
    // every time the i < length condition is checked.
    int length = strlen(string);

    for (i = 0; i < length; i++)
    {
        if (string[i] == ch)
        {
            count++;
        }
    }

    return count;
}

参见例如输入此code运行

See this code run on example input.

下面是你做错了什么:


  1. 既然你想找到一个的字符的,第二个参数应该是一个字符(而不是的char * ),这后来产生了影响(见#3)。

  2. 的sizeof(字符串)不给你字符串的长度。它提供了指针的大小(以字节为单位)在你的架构,这是的一个常数的(例如,4对32位系统)。

  3. 您是在比较一些值,它的的内存地址的内存地址 CHR 点。这是比较苹果和桔子,而总是返回,所以如果绝不会得逞的。

  4. 您想要做的,而不是什么是比较的字符的(字符串[我] )的函数的第二个参数(这是之所以一个也是字符)。

  1. Since you want to find a character, the second parameter should be a character (and not a char*), This has implications later (see #3).
  2. sizeof(string) does not give you the length of the string. It gives the size (in bytes) of a pointer in your architecture, which is a constant number (e.g. 4 on 32-bit systems).
  3. You are comparing some value which is not a memory address to the memory address chr points to. This is comparing apples and oranges, and will always return false, so the if will never succeed.
  4. What you want to do instead is compare a character (string[i]) to the second parameter of the function (this is the reason why that one is also a char).

下面评论者正确地确定这是不平常的方式来做事在C原来的答案的某些部分,可能会导致缓慢code,并有可能在错误下(诚然非凡)的情况。

A "better" version of the above

Commenters below have correctly identified portions of the original answer which are not the usual way to do things in C, can result in slow code, and possibly in bugs under (admittedly extraordinary) circumstances.

因为我相信正确实施 count_chars 可能是太涉及的人谁正在使用C他们的第一个步骤,我就只是附加在这里和离开了最初的回答几乎完好无损。

Since I believe that "the" correct implementation of count_chars is probably too involved for someone who is making their first steps in C, I 'll just append it here and leave the initial answer almost intact.

int count_chars(const char* string, char ch)
{
    int count = 0;
    for(; *string; count += (*string++ == ch)) ;
    return count;
}

注意:我特意写了循环这样做,在一些阶段,你必须绘制之间什么是可能的,什么是preferable行点

参见例如输入此code运行

See this code run on example input.

这篇关于计数中℃的字符串的字符出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆