/* functions.c Jeff Ondich, 26 April 2019 Part of a quick intro to C You'll have questions. Such as: what the heck does "const char *" mean? Write them down, discuss them, ask them! */ #include // These are called "prototypes" in C and C++. They're a way of // saying "there's going to be a function with this particular // interface, so you can go ahead and call these functions, but // they'll need to be implemented somewhere". int fibonacci(int n); int occurrences(char character, const char *string); int main() { // Let's print out some Fibonacci numbers. for (int k = 0; k < 15; k++) { printf("%d ", fibonacci(k)); } printf("\n\n"); // And here's a function that has a string paramater. char word[] = "Mississippi"; char character = 'i'; int occurrenceCount = occurrences(character, word); printf("There are %d occurrences of the character '%c' in the word \"%s\"\n", occurrenceCount, character, word); return 0; } // Returns the nth Fibonacci number. Assumes that the Fibonacci // numbers start at index 0, so fib0 = 1, fib1 = 1, fib2 = 2, etc. int fibonacci(int n) { int previous = 1; int current = 1; for (int k = 1; k <= n; k++) { int new = current + previous; previous = current; current = new; } return current; } // Returns the number of times character occurs in string. int occurrences(char character, const char *string) { int n = 0; for (int k = 0; string[k] != '\0'; k++) { if (string[k] == character) { n++; } } return n; }