Characters, Strings, and Arrays

Previously I wrote about primitive data types, and I’d like to expand on that by covering strings and arrays. For me, the easiest way to think of a string is as an array of characters; An array is just a collection of items that consist of a specific data type. In general, every character is a single bite and a single letter or number. There is a special escape character in C, ‘/0’, which is a single bite despite technically being two characters. The reason I think of strings as arrays is that there are a lot of methods you can perform on strings, such as calculating its length, creating substrings, or adding onto the string (concatenation). All of these things can typically be done with arrays too. In my experience it’s super useful to think of strings like this because it expands on your possibilities for handling strings.

When I took Application Development in Visual Basic (now C#) we had to design an application that could fix typos. I had asked my professor how to iterate through an array, and he responded with basically, “What?”. Me being me, I ended up hacking a solution to iterate through the array. The idea was to basically find the first instance of a hyphen or space, and then shorten the string starting at the next character. To make the string short and encapsulate all the special characters, it would stop at n+1 characters, where n is the last space or hyphen. My application ended up impressing him because it went above and beyond the class’s requirements. The assignment assumed a name had only two spaces or two hyphens due to the built-in library functions, but mine could work with any number of spaces or hyphens.

Leave a comment

Your email address will not be published. Required fields are marked *