Strings and Pointers

If you have not done so yet, create a new file stars.c, which you will gradually extend in the following exercises.

Splitting a String

Your first task is to process the identifiers of stars in the database. Star identifiers consist of two parts, the name of the constellation and the name of the star itself. The two parts are stored as a combined string and only separated by a colon (':').

You are supposed to implement the following function that takes a combined star identifiers and splits it into two parts:

char *splitPrefix(char *s, char delimiter);

The input to the function is a string s, representing a combined star identifier, and a delimiter character, which will be ':' in our case. The function should then:

  • Traverse the input string using a for loop and pointer arithmetic.
    • Initialize a new pointer to the value of s in the init expressions of the for loop.
    • The loop then should go on as long as the end of the string has not been reached.
      • Recall that, in C, strings are terminated by a null character ('\0').
      • Consequently you have to dereference the pointer using the * operator and check for a null character in the for loop's condition.
    • On each iteration advance the pointer using the post-increment operator ++ in the for loop's iteration expression.
  • Check for the occurrence of the delimiter character:
    • Dereference the pointer and check if the current character is a colon (':').
    • If so, replace it with a null character by dereferencing the pointer using the * operator ...
    • ... and return a pointer to the rest of the input string after the delimiter symbol.
  • Return some meaningful value after the for loop.

The idea is that after calling splitPrefix the prefix of the original input string is accessible using the pointer provided as input to the function, while the suffix is accessible using the returned pointer. The following figure illustrates the expected behavior on the example string "And:Alpheratz". The function's parameter s consequently points to the first character of the string, while another pointer traverses the string's characters until the delimiter symbol ':' is reached:

Initial string

Once the delimiter character is reached, it should be replaced by a null character and a pointer to the suffix of the initial input string should be returned, as illustrated by the following figure:

Test

Write a test function (chose an appropriate name), that allows you to verify whether the splitPrefix function, developed above, works correctly. Be careful to catch corner cases and unexpected inputs in your tests. Make sure that the splitPrefix function has a well-defined behavior and return value in such cases. Write a short comment to explain each of your test cases. The following code lines show an example test case:

char in1[] = "a:x";
char *out1 = splitPrefix(in1, ':');
printf("%s %s\n", in1, out1);

Finally, write a main function that calls the test function. Don't forget to add the necessary lines with #include at the beginning of your file. Make sure that the entire code compiles, runs, and works as expected.