Count the characters matching alphabets

Certified Senior Developer

Hello,

how to build an expression rule  which has 2 inputs - 1 is string, 2nd is alphabet. print the count of characters matching alphabet with the string

(Umbrella to expected Result -u1m1b1r1e1l2a1)

  Discussion posts and replies are publicly visible

Parents Reply Children
  • Sure, basically here we are splitting the input string into an array, utilizing union() to remove duplicate characters, and creating a string of each unique character plus the count of appearances in the original input.  Adding some comments as well:

    a!localVariables(
      local!alphabet: "abcdefghijklmnopqrstuvwxyz", /* input for which letters to consider */
      local!input: "Umbrella", /* string to evaluate */
      
      /* split the input string into an array of characters u-m-b-r-e-l-l-a */
      local!inputArray: a!forEach(
        items: 1+enumerate(len(local!input)),
        expression: lower(charat(local!input,fv!index))
      ),
      local!chars: union(local!inputArray,local!inputArray), /* array of unique characters only u-m-b-r-e-l-a */
      
      joinarray( /* combine forEach array into a single string */
        reject(
          fn!isnull, /* remove values not included in the input string */
          a!forEach(
            items: local!chars, /* loop unique characters */
            expression: if(
              find(fv!item,local!alphabet,1)>0, /* is this character in the alphabet string to consider? */
              concat( /* combine */
                fv!item, /* current character */
                count(wherecontains(fv!item,local!inputArray)) /* number of appearances */
              ),
              null
            )
          )
        )
      )
    )