Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming Exercises for the Third Week, Exercises of C programming

A series of exercises for the third week of c programming. The exercises cover various topics such as file i/o, string manipulation, and array processing. Students are expected to write programs to solve these exercises, some of which involve modifying existing code or creating new functions.

Typology: Exercises

2021/2022

Uploaded on 02/11/2022

ekaraj
ekaraj ๐Ÿ‡บ๐Ÿ‡ธ

4.6

(29)

264 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Programming, Exercises for the third week
1. What is the output of the following program?
#include <stdio.h>
int main( int argc, char *argv [ ])
{
int ch;
FILE *fptr;
fptr = fopen( argv[ 1 ], "r");
while ((ch = getc( fptr )) != EOF)
if (ch == โ€™ โ€™ || ch == โ€™\tโ€™)
putchar (โ€™\nโ€™);
else
putchar (ch);
fclose (fptr);
return 0;
}
2. Modify Example 3 of the third week to append user input to an existing text
file. So your program reads a user input (character string) from the keyboard
and appends the string to a text file.
3. Write a program that removes all empty lines (i.e. lines containing only the-
end-of-line character) from a text file. (Hint: It is easiest just to copy the text
file to another text file leaving the empty lines away. Notice the Windows
environment, too, i.e. \r\n.)
4. Write a program that counts the number of characters and words in a text file.
Use the command-line arguments to read the name of the file from a keyboard.
5. Write a program that reads character strings from a text file (strings are se-
parated with โ€™#โ€™ from each other) into two dimensional array and rearranges
them into alphabetical order, and then displays the rearranged list.
(Hint: It is possible to use the qsort sorting program, but this may be difficult,
because then you must use pointers in an tricky way. If you implement a sorting
algorithm yourself, for example bubble sort, then instead of an ordinary two
dimensional array it is better to use a one dimensional array whose elements
are pointers to strings. In this case you just swap pointers, it is not necessary
to copy strings. )
6. (The exercises from this onwards form a larger program.)
Create a function called is_line_break that takes a char value as input, and
outputs a c-like boolean value as output (1 for true, 0 for false). The value for
the output is defined by the given char value: if the value is โ€™\nโ€™ or โ€™\rโ€™ (line
change indicators), return 1, otherwise return 0.
1
pf3

Partial preview of the text

Download C Programming Exercises for the Third Week and more Exercises C programming in PDF only on Docsity!

C Programming, Exercises for the third week

  1. What is the output of the following program?

#include <stdio.h> int main( int argc, char *argv [ ]) { int ch; FILE *fptr; fptr = fopen( argv[ 1 ], "r"); while ((ch = getc( fptr )) != EOF) if (ch == โ€™ โ€™ || ch == โ€™\tโ€™) putchar (โ€™\nโ€™); else putchar (ch); fclose (fptr); return 0; }

  1. Modify Example 3 of the third week to append user input to an existing text file. So your program reads a user input (character string) from the keyboard and appends the string to a text file.
  2. Write a program that removes all empty lines (i.e. lines containing only the- end-of-line character) from a text file. (Hint: It is easiest just to copy the text file to another text file leaving the empty lines away. Notice the Windows environment, too, i.e. \r\n.)
  3. Write a program that counts the number of characters and words in a text file. Use the command-line arguments to read the name of the file from a keyboard.
  4. Write a program that reads character strings from a text file (strings are se- parated with โ€™#โ€™ from each other) into two dimensional array and rearranges them into alphabetical order, and then displays the rearranged list. (Hint: It is possible to use the qsort sorting program, but this may be difficult, because then you must use pointers in an tricky way. If you implement a sorting algorithm yourself, for example bubble sort, then instead of an ordinary two dimensional array it is better to use a one dimensional array whose elements are pointers to strings. In this case you just swap pointers, it is not necessary to copy strings. )
  5. (The exercises from this onwards form a larger program.) Create a function called is_line_break that takes a char value as input, and outputs a c-like boolean value as output (1 for true, 0 for false). The value for the output is defined by the given char value: if the value is โ€™\nโ€™ or โ€™\rโ€™ (line change indicators), return 1, otherwise return 0.
  1. Write a function get_file_length(char* filename) that determines and re- turns the length of a given file. Functions fopen, fseek, ftell and fclose are useful here. As file length can never be negative, what sort of return value should you use?
  2. Write a function char* load_data(char* filename) that returns the con- tents of a given file. If the load fails, make sure that you free the memory you have allocated for the data.
  3. You are given the following struct definition

struct map { int width; int height; char* data; };

Write a function struct map load_map(char filename) that loads in a map of the following format. The map is always rectangular, and its walls are defined by โ€™*โ€™. Sample map: (you should be able to copy-paste it into a file).





Hint: Use previous functions here, you can get the file length, data and line breaks from the previous tasks.

  1. Write a function print_map(struct map* the_map) that prints a map. The output should be as follows:

width: 12, height: 7





  1. Write a function char get_char_at(struct map *the_map, int x, int y) that returns the char value at a specific coordinate in the map. Define a bad character constant using the #define-command (e.g. #define BAD_CHAR โ€™Xโ€™) at the beginning of the source file, and return it if the given coordinates are outside of the map.