Question

In: Electrical Engineering

C programming review excerises. 1. Write a function that counts the number of lines in a...

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

Solutions

Expert Solution

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;

}


Related Solutions

Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write a function that counts the number of times a given integer occurs in a Linked...
Write a function that counts the number of times a given integer occurs in a Linked List. What is the time complexity of your algorithm? Justify your answer in python
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
Write a function in lisp XIT that counts the number of items in each sub-list of...
Write a function in lisp XIT that counts the number of items in each sub-list of a list and returns the count in a list. It should only work if there are two levels of brackets in the parameter (simple lists inside the parent list). Thus:             (XIT '((A B C)))        -> (3)             (XIT '((A) (A B) (A B C))) -> (1 2 3)             (XIT '((1 2)))              -> (2)             (XIT '(1 (2 3)))...
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing b) dangling reference
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is...
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is followed by a bit of the opposite parity (01 or 10) and writes the value to B. For example 00100110 has 4 cases: 00100110, 00100110, 00100110, 00100110.
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT