string operations: looking for a specified letter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ria3
    New Member
    • Mar 2007
    • 8

    string operations: looking for a specified letter

    Hi,
    How can I count the the number of times a specific letter like b occurs in a string?
    Thank you!!!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    How do you think you could do it? I could think of several ways.

    Comment

    • ria3
      New Member
      • Mar 2007
      • 8

      #3
      lol, I have no idea... that's why I posted here :P I need some help!

      Comment

      • sandyw
        New Member
        • Mar 2007
        • 122

        #4
        I'm a beginner myself.
        I would use an if statement to find b's
        along with What....
        sandy

        Comment

        • ria3
          New Member
          • Mar 2007
          • 8

          #5
          but.. how is that supposed to count it? And I don't know how many are in the string, the program is supposed to count that for me.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Well, you'd have to check each letter to see if it is a b - if it is, you can add 1 to a sum variable, and if not, don't do anything.

            Comment

            • hirak1984
              Contributor
              • Jan 2007
              • 316

              #7
              well just for hint...

              we have a function named chatAt() in java...
              try using that.
              Originally posted by ria3
              Hi,
              How can I count the the number of times a specific letter like b occurs in a string?
              Thank you!!!

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                If anyone had read the API docs for the String class the indexOf() method
                would've shown to be applicable for this job.

                Here's a link to the API docs.

                kind regards,

                Jos

                Comment

                • BSCode266
                  New Member
                  • Jan 2007
                  • 38

                  #9
                  If you look closely in the class String, which obviously i would have done first if i didnt know how to, you will find that there is a method charAt(); this one should help you out, yes this was said before just with a typo in it.

                  BSCode266

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by BSCode266
                    If you look closely in the class String, which obviously i would have done first if i didnt know how to, you will find that there is a method charAt(); this one should help you out, yes this was said before just with a typo in it.

                    BSCode266
                    I personally think that the indexOf method is better here, i.e. it keeps your
                    code simpler at no expense.

                    kind regards,

                    Jos

                    Comment

                    • hirak1984
                      Contributor
                      • Jan 2007
                      • 316

                      #11
                      indexOf() definitely helps,but I think it will return the first occurence of the alphabet searched for?

                      am I correct?

                      Originally posted by JosAH
                      I personally think that the indexOf method is better here, i.e. it keeps your
                      code simpler at no expense.

                      kind regards,

                      Jos

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by hirak1984
                        indexOf() definitely helps,but I think it will return the first occurence of the alphabet searched for?

                        am I correct?
                        Nope, sorry; there's an overloaded version available. Here's the spoiler:
                        Code:
                        int count= 0;
                        for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
                        return count;
                        kind regards,

                        Jos

                        Comment

                        • BSCode266
                          New Member
                          • Jan 2007
                          • 38

                          #13
                          You are right it does save you from working with booleans.

                          BSCode266

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            Originally posted by JosAH
                            Nope, sorry; there's an overloaded version available. Here's the spoiler:
                            Code:
                            int count= 0;
                            for (int i= 0; (i= indexOf(yourString, i)) != -1); i++, count++);
                            return count;
                            kind regards,

                            Jos
                            O.o

                            very clever. I hadn't thought of that. Maybe I need to get more creative with my loops...:)

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by Ganon11
                              O.o

                              very clever. I hadn't thought of that. Maybe I need to get more creative with my loops...:)
                              I'm just a bag full of tricks ;-)

                              kind regards,

                              Jos

                              Comment

                              Working...