Character Index

This is a warmup exercise. It is not compulsory, and may be completed individually or with your lab partner.

You should make sure you have completed readLine before completing this task.

Download charIndex.c, or copy it into your current directory on a CSE system by running

$ cp /web/cs1511/17s2/week05/files/charIndex.c .

Start by copying your readLine code into the provided function stub.

You are given a main function which reads in a string and a character. You are to implement the int charIndex(char c, char *string) function which takes a character and a string and returns the index (position) of the first appearance of the character in the string. If the character doesn’t appear in the string, you are to return NOT_IN_STRING.

Some Examples

Enter a line: Hello, world!
Enter a character: w
Index of character 'w': 7
Enter a line: aaa
Enter a character: a
Index of character 'a': 0
Enter a line: aaa
Enter a character: b
Character 'b' is not in the string.

To run some simple automated tests:

$ 1511 autotest charIndex

To run Styl-o-matic:

$ 1511 stylomatic charIndex.c
Looks good!

You’ll get advice if you need to make changes to your code.

Submit your work with the give command, like so:

$ give cs1511 wk05_charIndex

Or, if you are working from home, upload the relevant file(s) to the wk05_charIndex activity on Give Online.