2
$\begingroup$

I would like to write a function which produces a table of the frequencies with which various letters of the alphabet are found in some textual input.

Here is what I did:

In[212]:= 
    wordInput = "abcdasdagfkahjbfjasfjas3598713952/35'2.35;'235.f";
    freq[StringInput_] := 
      Tally[Table[
        StringTake[StringInput, {n}], {n, 1, StringLength[StringInput]}]];
    freq[wordInput]

Out[214]= {{"a", 6}, {"b", 2}, {"c", 1}, {"d", 2}, {"s", 3}, {"g", 
   1}, {"f", 4}, {"k", 1}, {"h", 1}, {"j", 3}, {"3", 5}, {"5", 
   5}, {"9", 2}, {"8", 1}, {"7", 1}, {"1", 1}, {"2", 3}, {"/", 
   1}, {"'", 2}, {".", 2}, {";", 
   1}}["abcdasdagfkahjbfjasfjas3598713952/35'2.35;'235.f"]

It did create a table of frequencies, but at the end it has this tail

["abcdasdagfkahjbfjasfjas3598713952/35'2.35;'235.f"]

How can I get rid of it?

$\endgroup$
2

1 Answer 1

3
$\begingroup$

I do not get the tail. You must have typed something else? This is version 11.1 also you do not need to do

 Table[StringTake[StringInput, {n}], {n, 1, StringLength[StringInput]}]

to split it. Just use StringSplit

wordInput = "abcdasdagfkahjbfjasfjas3598713952/35'2.35;'235.f";
freq[stringInput_] := Tally[StringSplit[stringInput, ""]];
Grid[freq[wordInput], Frame -> All]

Mathematica graphics

Update

Characters can also be used as follows, thanks to Pillsy suggestion in comment below

 freq[stringInput_] := Tally[Characters[stringInput]];
$\endgroup$
1
  • 2
    $\begingroup$ Characters is more perspicuous than StringSplit[..., ""] IMO. $\endgroup$
    – Pillsy
    Commented Apr 29, 2017 at 1:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.