C programming review excerises.
1. Write a function that counts the number of lines in a file,using the following declaration:
int countLines(char *filename)
2. Write a program that counts the number of words in a textfile. Use the command-line
arguments to read the name of the file. The syntax:
countwords filename.txt
3. Write a program that counts the number of words starting withthe first letter ‘T’ in a text
file. Using commend line argument for the text file. Syntax:
countWords filename.txt
4. Write a function that writes the multiplication table (from 1to 9) to a given file in the
following format:
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 … 1 x 9 =9
2 x 2 = 4 2 x 3 = 6 2 x 4 = 12 … 2 x 9 = 18
3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 … 3 x 9 = 27
…
9 x 9 = 81
Declaration of the function:
void printTable(char *filename)
5. Write a recursive function that computes the sum of allnumbers from 1 to n for a given
n, where n is a non-negative integer. Declaration of thefunction:
int sum(int n)
6. Write a recursive function that finds and returns thesmallest element in an array, where
the array and its size are given as parameters. Declaration:
int minimum(int array[ ], int n)
7. Write a recursive function that determines whether a givenarray is a palindrome. A
palindrome is a string of finite length that reads the samebackward as forward.
Declaration: int isPalindrome(int array[ ], int beginning, intend)
8. Write a recursive function that prints even numbers in anarray of integers where the
array and its length are given as parameters. Declaration:
void printEven(int array[ ], int len)
where len is non-negative.
9. Write a recursive function that prints the content of acharacter array in reverse order.
Declaration: void printReverse(char array[ ], int len)
where len is non-negative.
10. Write a recursive function that prints all integers in agiven range between start and end
in increasing order.
Declaration: void printRange(int start, int end)
where start <= end+1.
11. Write a function that returns the index of the firstoccurrence of a given character c in
string str. If the character is not found, return -1.
Declaration: int indexOf(char *str, char c)
12. Write a function that returns the index of the firstoccurrence of the given substring sub in
string str.
Declaration: int indexOf(char *str, char *sub)
If the substring is not found, return -1.
13. Write a function that returns the index of the lastoccurrence of character c in string str.
Declaration: int lastIndexOf(char *str, char c)
If the occurrence is not found, return -1.
14. Write a function that returns the character at position idxin string str.
If idx is beyond the index inside str, return -1.
Declaration: char charAt(char *str, int idx)
15. Write a function that returns 1 if string str starts withthe string sub, 0 otherwise.
Declaration: int startsWith(char *str, char *sub)
16. Write a function that returns a new string consisting of thesequence of characters
between the ith and jth index in the given string str. If this isnot possible, return NULL.
Declaration: char *substring(char *str, int i, int j)
17. Write a function that takes as input a string and returns anew string consisting of
repeating each characters in str. For example, if str is “helloâ€,the returned string should
be “hheellllooâ€;
Declaration: char *repeat(char *str)
18. Write a program that reads a list of x- and y-coordinatesfrom a file and stores it in an
array of type Point (defined as a struct with x and y floatmembers). You must use
typedef for the type Point. Assume the file contains at most 100pairs of x- and y-
coordinates. Syntax:
readXY filename
19. Reimplement program #18 but use malloc to dynamicallyallocate the array. Malloc 100
pairs at a time. If the file contained more points than theallocated space could fit,
reallocate 100 more space into the array. Continue till the entirefile is read. Syntax:
readXY filename