Home > documentation, Maxima, programming, symbolic computation > String processing with Maxima

## String processing with Maxima

The Maxima module stringproc.lisp provides many functions for working with and processing strings. You load the string processing module using the command load(stringproc). A string can be thought of as a sequence of characters delimited by double quotation marks. Something like “abc” is a valid string, but ‘abc’ is not a valid string because the sequence of characters abc is delimited by single quotation marks:

```(%i1) "abc";
(%o1)                                 abc
(%i2) 'abc';
Incorrect syntax: ' is not an infix operator
'abc';
^
```

When studying cryptography, it is useful to know about the ASCII integer corresponding to a character. The ASCII integer corresponding to the capital letter “A” is 65, that of its lower case equivalent is 97. Here is a list of the letters of the English alphabet and their corresponding ASCII integers:

```A 65      a 97
B 66      b 98
C 67      c 99
D 68      d 100
E 69      e 101
F 70      f 102
G 71      g 103
H 72      h 104
I 73      i 105
J 74      j 106
K 75      k 107
L 76      l 108
M 77      m 109
N 78      n 110
O 79      o 111
P 80      p 112
Q 81      q 113
R 82      r 114
S 83      s 115
T 84      t 116
U 85      u 117
V 86      v 118
W 87      w 119
X 88      x 120
Y 89      y 121
Z 90      z 122
```

You can work with these integers, performing various arithmetic operations on them. However, it is easier to work with the upper case letters and reduce their corresponding ASCII integers to values between 0 and 25, inclusive. Let’s load the module stringproc.lisp and use the function charat() to get characters of a string at specific indices:

```(%i12) load(stringproc)\$
(%i13) strUpper : "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
(%o13)                    ABCDEFGHIJKLMNOPQRSTUVWXYZ
(%i14) charat(strUpper, 1);
(%o14)                                 A
(%i15) charat(strUpper, 2);
(%o15)                                 B
(%i16) charat(strUpper, 3);
(%o16)                                 C
```

You should take note that Maxima starts counting from 1, not from 0.

The function cint() returns the ASCII integer corresponding to a character. To obtain the ASCII integers corresponding to the upper case English letters, you can use the function charlist() to convert a string to a list of characters, then apply cint() to each of those characters. Again, the function maplist() is very handy in this case. Recall that maplist() takes two arguments: the first argument is a function, in this case it’s the function cint(); and the second argument is a list of values that you want to apply that function to. Here is how you can obtain the ASCII integers corresponding to all the capital letters of the English alphabet:

```(%i23) cint("A");
(%o23)                                65
(%i24) cint("B");
(%o24)                                66
(%i25) cint("C");
(%o25)                                67
(%i26) L : charlist(strUpper);
(%o26) [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]
(%i27) maplist(cint, L);
(%o27) [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90]
```

This can be done in one line:

```(%i28) maplist(cint, charlist(strUpper));
(%o28) [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90]
```

Now to convert each of the above ASCII integers to values between 0 and 25, inclusive, you need to subtract 65 from each of those values. One way to do so is to define a function, call it to26(), that converts a character to its ASCII integer and subtract 65 from that integer. The result from to26() is an integer between 0 and 25, inclusive. Then you use maplist() to apply that function to a list of characters:

```(%i32) to26(x) := cint(x) - 65\$
(%i33) to26("A");
(%o33)                                 0
(%i34) to26("B");
(%o34)                                 1
(%i35) to26("C");
(%o35)                                 2
(%i36) maplist(to26, charlist(strUpper));
(%o36) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25]
```

Another way to do this is to use an un-named function, which can be achieved using the command lambda():

```(%i37) maplist(lambda([x], cint(x) - 65), charlist(strUpper));
(%o37) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25]
```

What about going the other way round? Say you have an integer between 0 and 25, inclusive. You want to convert that integer to its corresponding upper case letter. To do so, you reverse the above process as follows. If $n$ is an integer between 0 and 25, add 65 to $n$ and use the function ascii() to convert the result to an ASCII character:

```(%i38) n : 0;
(%o38)                                 0
(%i39) ascii(n + 65);
(%o39)                                 A
(%i40) n : 1;
(%o40)                                 1
(%i41) ascii(n + 65);
(%o41)                                 B
(%i42) n : 2;
(%o42)                                 2
(%i43) ascii(n + 65);
(%o43)                                 C
(%i44) L : maplist(lambda([x], cint(x) - 65), charlist(strUpper));
(%o44) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25]
(%i45) maplist(lambda([x], ascii(x + 65)), L);
(%o45) [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]
```

Notice that the result above is a list of characters. To convert that into a string, use the function simplode():

```(%i46) simplode( maplist(lambda([x], ascii(x + 65)), L) );
(%o46)                    ABCDEFGHIJKLMNOPQRSTUVWXYZ
```

These are just some of the commands in the string processing module stringproc.lisp of Maxima. For more string processing functions, have a look at chapter 76 of the Maxima manual.