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. Calculate the number of times a character occurs in a string

Calculate the number of times a character occurs in a string

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 3 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.
  • L Offline
    L Offline
    littlecheer
    wrote on last edited by
    #1

    Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer

    N P 3 Replies Last reply
    0
    • L littlecheer

      Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      Made function, no AFAIK. But you can code it quite easy. Take the String as a vector and make a for with an if and a counter. When you find the character... +1, when not... next element.

      Greetings. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson

      1 Reply Last reply
      0
      • L littlecheer

        Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer

        P Offline
        P Offline
        pierre_ribery
        wrote on last edited by
        #3

        Like this: std::string str("this is a test string only"); int num = std::count(str.begin(),str.end(),'a'); Remember: #include #include

        1 Reply Last reply
        0
        • L littlecheer

          Hi all, May I know how to calculate the number of times a character occurs in a string? Are there any function returns the number of times a character occurs in the string? Thanks. Regards, littlecheer

          P Offline
          P Offline
          pierre_ribery
          wrote on last edited by
          #4

          Like this: std::string str("this is a test string only"); int num = std::count(str.begin(),str.end(),'a'); Remember to include string and algorithm

          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