In: Electrical Engineering
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 text
file. 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 with
the 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 1
to 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 all
numbers from 1 to n for a given
n, where n is a non-negative integer. Declaration of the
function:
int sum(int n)
6. Write a recursive function that finds and returns the
smallest 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 given
array is a palindrome. A
palindrome is a string of finite length that reads the same
backward as forward.
Declaration: int isPalindrome(int array[ ], int beginning, int
end)
8. Write a recursive function that prints even numbers in an
array 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 a
character 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 a
given 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 first
occurrence 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 first
occurrence 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 last
occurrence 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 idx
in 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 with
the string sub, 0 otherwise.
Declaration: int startsWith(char *str, char *sub)
16. Write a function that returns a new string consisting of the
sequence of characters
between the ith and jth index in the given string str. If this is
not possible, return NULL.
Declaration: char *substring(char *str, int i, int j)
17. Write a function that takes as input a string and returns a
new 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-coordinates
from a file and stores it in an
array of type Point (defined as a struct with x and y float
members). You must use
typedef for the type Point. Assume the file contains at most 100
pairs of x- and y-
coordinates. Syntax:
readXY filename
19. Reimplement program #18 but use malloc to dynamically
allocate the array. Malloc 100
pairs at a time. If the file contained more points than the
allocated space could fit,
reallocate 100 more space into the array. Continue till the entire
file is read. Syntax:
readXY filename
Hello,
Please find
the answer to the first question attached as
under. Please give a thumbs up rating if you find
the answer useful! Have a rocking day ahead!
NOTE: You have posted a large number of questions which is impossible for the experts to answer in one go. Please post the questions one by one.
int countLines(char *filename)
{
int count = 0; // Line counter (result)
char c; // To store a character read from file
// Check if file exists
if (filename == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
fp = filename;
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
printf("The file %s has %d lines\n ", filename, count);
return 0;
}