string operations: looking for a specified letter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #16
    hey thnx buddy that is really tricky.

    :D

    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

    Comment

    • ria3
      New Member
      • Mar 2007
      • 8

      #17
      Thank you so much for your help Hirak, BSCode, and JosAH!
      I love the tricks on this forum, lol :)

      Comment

      • nmadct
        Recognized Expert New Member
        • Jan 2007
        • 83

        #18
        Originally posted by Ganon11
        very clever. I hadn't thought of that. Maybe I need to get more creative with my loops...:)
        I would argue that you should NOT think of that... "creative" code is harder to read!

        Code:
        int count = 0;
        int i = 0;
        while (true) {
           i = yourString.indexOf(i);
           if (i == -1)
              break;
           i++;
           count++;
        }
        return count;
        This does exactly the same thing but at a glance it's more obvious what it's doing.

        Comment

        • nmadct
          Recognized Expert New Member
          • Jan 2007
          • 83

          #19
          Not that I'm trying to spoil your cleverness, hehe!

          Also, I meant to write: i = yourString.inde xOf(charToCount , i);

          So it works instead of looping infinitely.

          Comment

          • hirak1984
            Contributor
            • Jan 2007
            • 316

            #20
            ohk... ohk....
            cool down..

            no more arguments,plz. :D
            Originally posted by nmadct
            I would argue that you should NOT think of that... "creative" code is harder to read!

            Code:
            int count = 0;
            int i = 0;
            while (true) {
               i = yourString.indexOf(i);
               if (i == -1)
                  break;
               i++;
               count++;
            }
            return count;
            This does exactly the same thing but at a glance it's more obvious what it's doing.

            Comment

            • sandyw
              New Member
              • Mar 2007
              • 122

              #21
              What did I start the battle of "Counters"! !!
              You got to love it,

              sandy

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #22
                Originally posted by nmadct
                I would argue that you should NOT think of that... "creative" code is harder to read!

                Code:
                1: int count = 0;
                2: int i = 0;
                3: while (true) {
                4:    i = yourString.indexOf(yourChar, i);
                5:    if (i == -1)
                6:       break;
                7:    i++;
                8:    count++;
                9: }
                This does exactly the same thing but at a glance it's more obvious what it's doing.
                That's all in the eye of the beholder (I numbered your code lines and fixed the
                little typo). The way I think goes like this:

                1: and 2: ok initialization stuff
                3: ok, an infinite loop
                4: huh? ah, got it.
                5: 6: double huh? So it isn't in infinite loop after all; so he wanted to say:
                Code:
                while ((i= yourString.indexOf(yourChar, i)) != -1) ...
                7: 8: See? that's the increment part of the loop, so the following would've done
                the job:
                Code:
                for( ; (i= yourString.indexOf(yourChar, i)) != -1; i++, count++);
                (staring at the top of the page again: aha! that 'i' variable just has a loop scope
                so this is what he meant:
                Code:
                for(int i= 0 ; (i= yourString.indexOf(yourChar, i)) != -1; i++, count++);
                kind regards,

                Jos ;-)

                Comment

                • prometheuzz
                  Recognized Expert New Member
                  • Apr 2007
                  • 197

                  #23
                  Ha, you people with your multy line solutions!
                  Code:
                  /**
                   * @param s     the string to look for
                   * @param t     the text to look through for 's'
                   * @return      the number of times 's' is in 't'
                   */
                  public static int count(String s, String t) {
                    return (t.length()-t.replaceAll(s, "").length())/s.length();
                  }
                  ; )

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by prometheuzz
                    Ha, you people with your multy line solutions!
                    Code:
                    /**
                     * @param s     the string to look for
                     * @param t     the text to look through for 's'
                     * @return      the number of times 's' is in 't'
                     */
                    public static int count(String s, String t) {
                      return (t.length()-t.replaceAll(s, "").length())/s.length();
                    }
                    ; )
                    I did expect something to be casted to double though given a gory solution
                    like that; and then some ...

                    kind regards,

                    Jos ;-)

                    Comment

                    Working...