how to count the occurance of a character in a string ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.hku.hk

    how to count the occurance of a character in a string ?

    how to count the occurance of a character in a string ?

    e.g.
    #include <iostream>
    #include <string>
    using namespace std;
    int main (){
    string text = "abc, def, ghi, jkl, mno";
    // how to count the number of occurance of "," in text ????
    return 0;
    }

    thanks


  • Siemel Naran

    #2
    Re: how to count the occurance of a character in a string ?

    "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message news:408a92c6
    [color=blue]
    > how to count the occurance of a character in a string ?[/color]

    std::count


    Comment

    • news.hku.hk

      #3
      Re: how to count the occurance of a character in a string ?

      but it said "count" is undeclared identifier..... .
      shall i include any library or do anything?


      "Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote in message
      news:sUwic.3413 3$um3.675558@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
      > "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message news:408a92c6
      >[color=green]
      > > how to count the occurance of a character in a string ?[/color]
      >
      > std::count
      >
      >[/color]


      Comment

      • John Harrison

        #4
        Re: how to count the occurance of a character in a string ?


        "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message
        news:408a92c6$1 @newsgate.hku.h k...[color=blue]
        > how to count the occurance of a character in a string ?
        >
        > e.g.
        > #include <iostream>
        > #include <string>
        > using namespace std;
        > int main (){
        > string text = "abc, def, ghi, jkl, mno";
        > // how to count the number of occurance of "," in text ????
        > return 0;
        > }
        >
        > thanks
        >[/color]

        It's very simple, in fact it couldn't be easier. Loop though the string
        looking for commas.

        int count = 0;
        for (int i = 0; i < text.size(); ++i)
        if (text[i] == ',')
        ++count;

        john


        Comment

        • Karthik

          #5
          Re: how to count the occurance of a character in a string ?

          news.hku.hk wrote:
          [color=blue]
          > how to count the occurance of a character in a string ?
          >
          > e.g.
          > #include <iostream>
          > #include <string>
          > using namespace std;[/color]
          Think twice before u 'use' the entire namespace. It would cause name
          pollution which a probable user of the API won't even notice.

          Aliter:

          #include <string>
          #include <iostream>

          typedef unsigned int UINT;

          UINT ncount(std::str ing text, char ch) {
          UINT count = 0;

          int index = -1;
          while (1) {
          index = text.find_first _of(ch, index + 1);
          if (index == text.npos) break;
          else count++;
          }
          return count;

          }

          Karthik
          ------
          Human Beings please 'removeme' for my email.

          Comment

          • James Moughan

            #6
            Re: how to count the occurance of a character in a string ?

            "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message news:<408a92c6$ 1@newsgate.hku. hk>...[color=blue]
            > how to count the occurance of a character in a string ?
            >
            > e.g.
            > #include <iostream>
            > #include <string>
            > using namespace std;
            > int main (){
            > string text = "abc, def, ghi, jkl, mno";
            > // how to count the number of occurance of "," in text ????
            > return 0;
            > }
            >
            > thanks[/color]

            Learn to use the elements of the algorithms package for stuff like
            this; it gives you some great high-level methods.
            http://www.josuttis.com/libbook/toc.html is the best site I've found
            covering these things.

            For this problem use count;

            #include <iostream>
            #include <string>
            #include <algorithm>

            using namespace std;
            int main (){
            string text = "abc, def, ghi, jkl, mno";

            //num_commas holds the number of elements in text == ','
            int num_commas = count(text.begi n(), text.end(), ',');

            return 0;
            }

            Comment

            • rossum

              #7
              Re: how to count the occurance of a character in a string ?

              On Sun, 25 Apr 2004 13:00:23 +0800, "news.hku.h k"
              <billychu@hkusu a.hku.hk> wrote:
              [color=blue]
              >but it said "count" is undeclared identifier..... .
              >shall i include any library or do anything?
              >
              >
              >"Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote in message
              >news:sUwic.341 33$um3.675558@b gtnsc04-news.ops.worldn et.att.net...[color=green]
              >> "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message news:408a92c6
              >>[color=darkred]
              >> > how to count the occurance of a character in a string ?[/color]
              >>
              >> std::count
              >>
              >>[/color]
              >[/color]

              You need to include <algorithm> for std::count()
              The STL is described at http://www.sgi.com/tech/stl/

              #include <iostream>
              #include <string>
              #include <algorithm>

              int main ()
              {
              std::string text = "abc, def, ghi, jkl, mno";
              std::cout << "Number of commas = "
              << std::count(text .begin(), text.end(), ',')
              << '\n';
              return EXIT_SUCCESS;
              } // end main()

              ### Warning - not compiled so may have small errors. ###

              rossum
              --

              The Ultimate Truth is that there is no Ultimate Truth

              Comment

              • Michiel Salters

                #8
                Re: how to count the occurance of a character in a string ?

                "Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote in message news:<sUwic.341 33$um3.675558@b gtnsc04-news.ops.worldn et.att.net>...[color=blue]
                > "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message news:408a92c6
                >[color=green]
                > > how to count the occurance of a character in a string ?[/color]
                >
                > std::count[/color]

                I've noticed a number of people posting alternatives. std::count
                is superior, for a number of reasons:

                * It's tested
                * It's documented
                * Someone reading the code later immediately sees the intent
                * It may be optimized for your particular implementation
                using non-portable tricks (e.g. written in assembly).

                The last argument is slightly tricky. You could write similar
                optimized code, but then the first three points apply even more.
                Worse, your code would be non-portable. Had you used std::count,
                you would get a different std::count when you actually port.

                Of course, std::count is O(n) so performance is good enough unless
                profiling proves otherwise.

                Regards,
                Michiel Salters

                Comment

                Working...