2

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?

Henry Wang
  • 359
  • 2
  • 8

1 Answers1

3

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]];
Nasser
  • 143,286
  • 11
  • 154
  • 359