How to count the repeated alphabet in sketch

edited December 2015 in Questions about Code

I've a set of string it's a music lyric and l want to count how many repeated alphabets like how many A, B,C,D...

but after I searching around I only found the way to count the string not alphabetical letters. is there's anyway to do it?

thank you.

Answers

  • edited November 2015

    @benja Thank you man!

  • you need to convert your lyrics to all upper case first I guess

    you then need an Array of int to hold your counts for each letter, e.g. using ascii as an index

    int[] countLetters = int[255]; 
    
    for (int i=65; i<65+26; i++) {
        countLetters[i] += ...................; 
    }
    
  • and have a test case instead of real lyrics

    String SENTENCE = 
      "The unusually quick brown fox " +
      "jumps over the lazy family dog repeatedly.";
    
  • edited December 2015

    Here, I tried another code from benja's link @Chrisir.

        String asentence = 
        "The unusually quick brown fox " + "jumps over the lazy family dog repeatedly.";
        String[] Sentence;
        String delimiters = " ',.?!;:\n()*";
        //String aSENTENCE = splitTokens(SENTENCE, delimiters);
        String noSpaces = "";
    
        void setup() {
          Sentence = splitTokens(asentence, delimiters);
          for (String s : Sentence) {
            noSpaces += s;
          }
          char[] letters = noSpaces.toCharArray();
          String upLetters = new String(letters);
          upLetters = upLetters.toUpperCase();
    
          textSize(32);
          text(upLetters, 10, 30);
    
          int[] letterC = new int[26];
          char[] alphabet = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
          };
          println(alphabet.length);
          for (int i=0; i< alphabet.length; i++) {
            letterC[i] = upLetters.length() - upLetters.replace(alphabet[i]+"", "").length();
            println(alphabet[i]+" is " +letterC[i]);
          }
        }
        void draw() {
        }
    
  • edited December 2015

    now letterC[] holds the count of each letter. well done.

    now you need to combine the 2 sketches:

    • make one setup() out of the 2 setup(), make one draw() (which is easy since draw() here is empty ;-) )

    • (the counting sketch here is a helper sketch for the alphabet sketch)

    then when drawing the alphabet in the other sketch read the info from letterC[] for that letter and draw it a few times with an offset

    int offsetX = 0; 
    
    for (int iLetterCount = 0; iLetterCount < letterC [i]; iLetterCount++) {
        text(...., x+offsetX, y); 
        offsetX--; 
    }
    

    (edited)

  • @Chrisir Before going to next step,I have a question. After I combined them together but then it seems like it counted itself repeated 28 times in println.. I don't understand why

    PFont font; //from Draw A-Z
    String asentence = "The unusually quick brown fox " + "jumps over the lazy family dog repeatedly.";
    String[] Sentence;
    String delimiters = " ',.?!;:\n()*";
    //String aSENTENCE = splitTokens(SENTENCE, delimiters);
    String noSpaces = "";
    
    //from Draw A-Z
    class LetterBlock {
      char a;
      float x, y;
      int count;
      LetterBlock(char ia, float ix, float iy) {
        a=ia;
        x=ix;
        y=iy;
        count=0;
      }
      void draw() {
        noFill(); 
        stroke(255); 
        font = createFont("FreeSans.ttf", 100);
        textFont(font);
        fill(255);
    
        //stroke(255);
        if ( mouseOver() ) fill(128, count, 255);
    
        //textSize(random(50));
        text(a, x, y);
      }
      boolean mouseOver() {
        return(dist(mouseX, mouseY, x, y)<=20);
      }
      void click() {
        if ( mouseOver() ) count+=20;
      }
    }
    
    LetterBlock[] lbs = new LetterBlock[26]; 
    
    
    
    void setup() {
      size(800, 600);
      //drawA-Z
      textAlign(CENTER, CENTER);
      smooth(); 
      int x = 0;
      for (int j=0; j<4; j++) {
        for (int i=0; i<7; i++) {
          if (!(j==3 && (i==0 || i ==6))) {
            lbs[x] = new LetterBlock(char('A'+x), 50+115*i, 50+150*j);
            x++;
          }
    
          //counting alphabet  
          Sentence = splitTokens(asentence, delimiters);
          for (String s : Sentence) {
            noSpaces += s;
          }
          char[] letters = noSpaces.toCharArray();
          String upLetters = new String(letters);
          upLetters = upLetters.toUpperCase();
    
          //textSize(32);
          //text(upLetters, 10, 30);
    
          int[] letterC = new int[26];
          char[] alphabet = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
          };
          println(alphabet.length);
          for (int ia=0; ia< alphabet.length; ia++) {
            letterC[ia] = upLetters.length() - upLetters.replace(alphabet[ia]+"", "").length();
            println(alphabet[ia]+" is " +letterC[ia]);
          }
        }
      }
    }
    void draw() {
     /*
      background(0);
      for (int i=0; i<lbs.length; i++) lbs[i].draw();
    
      //repeated letters
    
      int offsetX = 0; 
      for (int iLetterCount = 0; iLetterCount < letterC [ia]; iLetterCount++) {
        text('A', x+offsetX, y); 
        offsetX--;
      }
      */
    }
    
  • check your {} pairs in setup() please

  • it might be cool to play the song while looking at the screen

    you could also like register only the current line in the lyrics.

    thus the alphabet would change over the song from line to the next line of the lyrics

  • declare this int[] letterC before setup since you want to use it in draw() as well

Sign In or Register to comment.