Character Arrays

The questions below are due on Monday October 12, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercises

The char data type is an integral type, meaning the underlying value is stored as an integer. Similar to how a Boolean value 0 is interpreted as false and 1 is interpreted as true, the integer stored by a char variable is interpreted as an ASCII character.

ASCII stands for American Standard Code for Information Interchange, and it defines a particular way to represent English characters (plus a few other symbols) as numbers between 0 and 127 (called an ASCII code or code point). For example, ASCII code 97 is interpreted as the character ‘a’. The chart of printable characters mapping their number value to their symbols can be found here.

1) chars have integer values

For each of the following, please answer in decimal what the value of result will be after running the piece of code (consider each separately).

char six = '6';
int result = six + 0;

Decimal value:

char greeting[7] = "hello!"; // length of char_array hello! is 6 characters + null character
int result = 0;
for (int i=0; i<6; i++) result += greeting[i]; 

Decimal value or mathematical expression:

2) Capitalize char_array

2.1) In-place

Write a function that takes in a char array and converts all the lowercase letter characters to uppercase letters (a-z to A-Z). Note the void return type of this function. This function should manipulate the char array passed in as input.

Hint: What is the ASCII integer value of 'a' and 'A'? What is the numerical difference between the two? How about 'b' and 'B'?

2.2) Dedicated output

It's not always best to write a function that modifies its input. In many cases we would want to have a separate output argument that we can store our result to instead. Now, write the same function that takes in a char array input to capitalize, but also takes an output char array that will be populated instead of the input. For now, you may assume it is at least as large as the input char array and is empty.

3) camelCase to snake_case

3.1) Find humps

Now that you have had some practice with changing characters through their ASCII integer representations, let's put this to use! Per wikipedia, "naming convention is a set of rules for choosing the character sequence to be used for identifiers...". Two common naming conventions you will see are camelCase and snake_case.

First, let's write a function that can identify the number of humps that need to be replaced with underscores. A hump in camelcase is defined as an uppercase letter between two lowercase letters and is never found at the start. However, a hump can be at the end if the ending character is an uppercase letter preceded by a lowercase letter.

In completing this task, we recommend writing a helper function int is_a_hump(char* char_array, int index) to determine if char_array[index] (or, equivalently, *(char_array + index)) is a hump. In particular, it should return 1 when char_array[index] is a hump, or 0 otherwise. Your implementation of this function will not be graded, however.

3.2) In-place

Using the function you just implemented to get the number of humps in a char array, write a function that modifies the input char array (in-place) and converts any instance of camelCase to snake_case. Remember, this is done through inserting an underscore before a hump and changing the hump letter to lowercase. For now, you may assume that char_array_in is big enough to insert new chars, effectively increasing its length.

For this task, you can call int number_of_humps(char* char_array_in) and int is_a_hump(char* char_array, int index) (index should be between 0 and n-1, inclusive, where n is the length of the string).

Hint: Since you are inserting new characters, you will have to shift existing ones over to make space for '_'. It might be helpful to first find the length of the resulting char array and start at the end of that length, going backwards.

3.3) Dedicated output and proper implementation

Up to this point, you have gotten lucky with your function implementations, in the sense that you were allowed to assume the char_arrays used for output had enough room and could be modified (in the case of modifying the input) or were empty (in the case of a separate output). This is not the best practice and can lead to big issues (writing over memory) when you don't know the max number of elements you are allowed to store in output arrays. This is why when we have an output array we are writing to, it is good and proper practice to also take in an output length indicating how many elements we can store before we must stop.

Now, write a function that converts any instance of camelCase in the input array to snake_case in the output array. Essentially, your code should replace all humps with an underscore preceding hump, and making the hump lowercase. Unlike the previous functions in this exercise (that had poor practice), we will also be taking an output length. Your function may not write more characters than the given output_length to the output char array. Remember to finish off your output with the null character ('\0' or 0) if output_length permits.

For this task, you likely won't need to use the number_of_humps function, so we will not provide it for you. However, you can still call int is_a_hump(char* char_array, int index) (again, index should be between 0 and n-1, inclusive, where n is the length of the string).

Back to Exercises