Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get character count

How to get character count

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R raju_shiva

    Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

    H Offline
    H Offline
    HTT90
    wrote on last edited by
    #2

    You can use function strtok . Have fun .

    1 Reply Last reply
    0
    • R raju_shiva

      Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

      S Offline
      S Offline
      Sauro Viti
      wrote on last edited by
      #3

      What's about this:

      int GetCommaCount(const CString &str)
      {
      int count = 0;
      for(int pos = 0; pos < str.GetLength(); pos++)
      {
      if (str[pos] == _T(','))
      count++;
      }
      return count;
      }

      1 Reply Last reply
      0
      • R raju_shiva

        Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        If you use the CStringT class you can do it via the SpanIncluding()[^] method.

        It's time for a new signature.

        S 1 Reply Last reply
        0
        • R raju_shiva

          Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #5

          std::count_if is another method. Something like

          bool is_comma(char c)
          {
          return c == ',';
          }

          std::string s = "1,2,3,4";

          int count = std::count_if(s.begin(), s.end(), is_comma);

          Edit for clarification: This algorithm can of course be applied to a CString as well using the GetBuffer() member.

          home

          modified on Thursday, August 26, 2010 5:31 AM

          C 1 Reply Last reply
          0
          • L Lost User

            If you use the CStringT class you can do it via the SpanIncluding()[^] method.

            It's time for a new signature.

            S Offline
            S Offline
            Sauro Viti
            wrote on last edited by
            #6

            It's not so true because, as the MSDN documentation says, SpanIncluding returns an empty substring if the first character in the string is not in the specified set.

            L 1 Reply Last reply
            0
            • R raju_shiva

              Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              Assuming your string is

              CString str = _T("Sample(10,20,30,40,50)");

              Then

              int start = -1;
              int count = 0;
              while ( (start = str.Find(_T(','), start + 1)) != -1)
              count++;

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • N Niklas L

                std::count_if is another method. Something like

                bool is_comma(char c)
                {
                return c == ',';
                }

                std::string s = "1,2,3,4";

                int count = std::count_if(s.begin(), s.end(), is_comma);

                Edit for clarification: This algorithm can of course be applied to a CString as well using the GetBuffer() member.

                home

                modified on Thursday, August 26, 2010 5:31 AM

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #8

                Of course you don't need GetBuffer:

                #include <algorithm>
                using namespace std;
                bool is_comma(TCHAR c)
                {
                return c==_T(',');
                }

                LPCTSTR beg = (LPCTSTR) str;
                LPCTSTR end = beg + str.GetLength();
                int result = count_if(beg, end, is_comma);

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                N 1 Reply Last reply
                0
                • C CPallini

                  Of course you don't need GetBuffer:

                  #include <algorithm>
                  using namespace std;
                  bool is_comma(TCHAR c)
                  {
                  return c==_T(',');
                  }

                  LPCTSTR beg = (LPCTSTR) str;
                  LPCTSTR end = beg + str.GetLength();
                  int result = count_if(beg, end, is_comma);

                  :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  N Offline
                  N Offline
                  Niklas L
                  wrote on last edited by
                  #9

                  You're quite right!

                  home

                  1 Reply Last reply
                  0
                  • S Sauro Viti

                    It's not so true because, as the MSDN documentation says, SpanIncluding returns an empty substring if the first character in the string is not in the specified set.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Oops!

                    It's time for a new signature.

                    1 Reply Last reply
                    0
                    • R raju_shiva

                      Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      raju_shiva wrote:

                      i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5

                      Shouldn't that be 4? :confused:

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Man who follows car will be exhausted." - Confucius

                      1 Reply Last reply
                      0
                      • R raju_shiva

                        Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj

                        _ Offline
                        _ Offline
                        _Superman_
                        wrote on last edited by
                        #12

                        You can use the CString::Tokenize[^] method by specifying "," as the token delimiter. You can get the count by incrementing a variable in the while loop in the example in the above link.

                        «_Superman_»
                        I love work. It gives me something to do between weekends.

                        Microsoft MVP (Visual C++)

                        Polymorphism in C

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups