Where should I practice C.

Exercises in programming in C


3rd task


One about the Standard input to be entered text should analyzed become.

Create a ANSI-C program, with the

  • the number of lines,
  • the number of characters in the longest line,
  • the number of words,
  • the length of the longest word of the input text can be determined.

Under a word every character string that begins with a letter or underscore ('_') consists only of letters, digits and the underscore and is delimited by any control or special characters (but not digits).
Note that according to this definition, e.g. B. the string ; 2abc% contains no word.

  • Notes on the solution :
    • The text must be read in with the getchar () function and must not be stored in arrays.
    • For reasons of simplification (and portability) it can be assumed that the German umlauts do not appear in the text.
    • Note that the last line does not have to end with an end-of-line character.
      On the other hand, an end-of-file character (CTRL-Z under MS-DOS) does not represent a new line immediately after the end of a line.
    • To make word matching easier, are the following symbolic constants to be defined and to be used sensibly:
      • symbolic constant to identify the respective Character class
        (Upper and lower case letters, underscore)
        (Digits)
        (remaining characters, i.e. control characters and other special characters)
      • symbolic constant to identify the current one Text state
        (next character can be the beginning of a word)
        (within a word)
        (within a character string that is not a word, the next character cannot be the beginning of a word)
    • Think about the state transition triggered by a character depending on its class and the old text state.
      Draw a State transition diagram.
      The diagram must be submitted when the program is submitted.
    • Describe the algorithm to be implemented by the program before formulating it in C. Structogram.
      The structured chart must be submitted when the program is submitted.


4th task


Create a ANSI-C program, with the one histogram the Word length one about the Standard input entered Text can be determined and displayed on the screen in the following form:

H istogram of the word length of a text -------------------------------------------- --- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ----------------------------- ---------------------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Under a word any character string that begins with a letter or underscore ('_'), consists only of letters, digits and the underscore and is delimited by any control or special characters (but not digits), is understood.
Note that according to this definition, e.g. B. the string ; 3abc% contains no word.

The number of asterisks should be equal to the number of occurrences of a word of the corresponding length.
All words that are greater than or equal to 25 characters are to be grouped together.

In order for the entire screen output to be visible, word lengths whose appearances exceed 20 must be limited to 19 asterisks and the actual number of appearances must be displayed at the position of the 20th (top) asterisk.

  • Notes on the solution :
    • Use the algorithm developed in Task 3 for word matching
    • Insert to record the appearances of each word length int array whose components are expediently indexed using the word length.
    • The screen output must not take place via an absolute cursor positioning.


5th task

  • Formulate one ANSI-C function which searches the string for the first occurrence of the string and returns the following function value:
    Function value:
    - index,which the first character of in has when it is completely contained in (first index: 0)
    - -1,if not "completely" (or not at all) in.
  • Formulate one ANSI-C function with which the string "str" ​​is read from the standard input.
    The function should work like the ANSI-C standard function - except for the generation of function values.
    Function value:
    - String length,when the entry has been completed with
    - EOF,when the input has been completed with CTRL-Z
  • Test the above features with a small one ANSI-C program.
    Command prompt - from the standard input first read in a target string and then a search string and
    -if it is included, the index of the first occurrence of the search string in the target string
    -or if it is not included, the message "Not included!"
    output.
    The read-in loop and thus the program should be terminated if CTRL-Z is entered at any point.
  • Notes on the solution :
    • Search and target strings can have a maximum length of 80 characters.
      The function should not check whether a maximum permissible string length is exceeded.
    • Both functions must not use global variables.
    • Library functions for string processing must not be used
    • Upper and lower case letters are to be regarded as different
    • When using the function, take into account that the final NUL character ('\ 0') does not belong to the actual string and should therefore not be included in the search.
      Think about a sensible behavior for the transfer of empty strings.
    • Use the conditional evaluation operator to determine the functional value of and


6th task


Develop an ANSI-C program that reacts to the input of abbreviations for the - German - weekdays with the output of the full - English - name of the weekdays.

  • The abbreviations for the days of the week should consist of 3 characters and read as follows: MON, TUE, WED, DON, FRI, SAT, SUN.
    No distinction should be made between upper and lower case letters, i.e. instead of "MON", for example, "mon" or "Mon" or "moN" etc. can also be used.
  • The program should respond to the input of a wrong - even too short - abbreviation with the output of "No_day".
  • The program is to request the input of a new abbreviation name in a new line in a loop and generate the corresponding output in the next line.
    Complete each entry with .
  • If more than three characters are entered, only the first three characters are to be taken into account. The rest of the input line is to be ignored.
  • The program should end when an end-of-file character (under MS-DOS: ) is entered as the first character after a prompt.
  • Example of a program dialog: Enter the abbreviation for a weekday:? mon -> Monday? mo -> No_day? Tuesday -> Tuesday? ^ Z
  • Notes on the solution :
    • Structure the program as sensibly as possible into suitable functions.
      Among other things, the following functions are to be developed and used:
      Reading a string of maximum length m into the variable s (from stdin), skipping any further characters up to the end of the line.
      Function value:
      1st character read or
      Conversion of all lower case letters in the string s to upper case letters

      To actually determine the full English name from the German abbreviation are a or two other suitable functions to be defined and implemented.

    • Put appropriate 2-dimensional char arrays to save the abbreviations and the full weekday names.


7th task


Create an ANSI-C program with which arithmetic expressions entered via the keyboard can be evaluated (pocket calculator!).

Example:

    Input: (4 + 7.5 * 3) * 2-5.5 * 4 =
    Edition: 31.000000

The program should continuously request the input of an expression in a loop, read it in, evaluate it and output the result on the screen.
It should end when an end-of-file character (CTRL_Z) is entered at any point.

If you enter an incorrect expression or a denominator with the value "0", the program should respond with an appropriate error message instead of a result.
A distinction should be made between the following error messages:

  • "Closing bracket missing"
  • "Other syntax error"
  • "Denominator is 0"

Hints :

  • An expression can be syntactically with the following. Rules are described:
      Expression :: == term {+ term || -Term} Term :: == Factor {* Factor || / Factor} factor :: == number || (Expression) number :: == [-] digit {digit} [. {Digit}]
  • Except within a number, whitespace characters (blank, newline, tab) may appear anywhere in the printout.
  • Please note that an expression in the above sense is made up of floating point numbers, but these may only be entered in the "comma" notation and not in the exponent notation.
  • The input of an expression should be concluded with an "=" but not with a , i.e. an expression can extend over several lines.
  • Only use the function for reading in, not the or functions.
  • Use suitable functions that emulate the above rules to evaluate an expression. (Note the included recursion!)
    These functions must neither have parameters nor access global variables.
    Each function must read in the characters it processes itself.
    If it encounters a character that does not correspond to the syntax it has processed, it must be terminated after the character has been returned to the standard input (!).
  • To identify an error, you can use the option of returning a character other than the last read character with ungetc (), for example.


8th task


Create a program with a maximum of 1000 Strings the maximum length 136 Alphabetical can be sorted, whereby uppercase and lowercase letters should be regarded as the same.
Use the to sort Quick sort algorithm.

By specifying "on" or. "from"In the command line, when the program is called, it should be determined whether the sorting is in ascending or descending order.
If no command line parameter or no permissible command line parameter is specified, the sorting must be in ascending order.

The strings to be sorted are read in from stdin.

The sorted strings are to be output to stdout.

Hints :

  • The entry of each string is terminated by a line end.
    However, the end of the line itself no longer belongs to the string.
    The entry of the strings is terminated by an end-of-file character (CTRL-Z).
  • Only as much memory space as is actually required is to be allocated for the individual strings.
  • The program is to be designed modularly by defining and using suitable functions.
    Among other things, the following three functions must be formulated:
    Reading in the strings
    Sort the strings
    Output of the strings

    After determining the sorting direction, essentially only these three functions can be called in the function.

  • The source program must be formulated in such a way that it can be parameterized against a change in the maximum number of strings and the maximum string length.
  • The program should also end without errors if the input is immediately terminated with an end-of-file character (CTRL-Z), i.e. if no strings are read in at all.
  • The parameter "up" or "down" determines the actual sorting direction and not just the output direction of the strings.
  • Use the redirection of the standard input from any text file for testing when the program is called.


9. Task


In a file, the name of which can change, there are an unknown number of unsorted "double"Values ​​in machine-internal representation.

Create an ANSI-C program with which the values ​​can be read and stored internally in a binary tree structure sorted in ascending order.

The sorted sequence of values ​​is then standardized to the maximum value and output in a text file - one value per line.
The name of the text file should also be variable.

The number of read values, the maximum value and the minimum value (each not normalized) are to be displayed on the screen.

The names of the input (double value) file and the output (text) file are to be passed as command line parameters.
If only one or no command line parameter is passed, the program must be terminated with an indication of the correct call format.

If the input file cannot be opened (e.g. because it does not exist) or the output file cannot be opened, the program must be terminated with a corresponding message that must contain the file name.

A suitable file with the "double" values ​​is provided for test purposes.

Hints :

  • The program is to be designed modularly by defining and using suitable functions.
    Among other things, the following three functions must be formulated:
    Reading in and sorted storage of the double values
    Output of the normalized double values ​​in a text file
    Output of the information to be output on the screen about the numbers read

    If necessary, these functions can call other functions to be defined.


10th task


Create a C programDUMP with which the content of any file can be displayed in hexadecimal and in ASCII interpretation on the screen (see illustration below). Control characters and characters with a code> = $ 80 must be displayed as a point in the ASCII interpretation.
The name of the file is to be passed as a command line parameter.
If this information is missing, the program must ask for the file name.
If the specified file cannot be opened, the program must be terminated with a corresponding message being output.

The dump is by the page (16 lines of 16 bytes each). After displaying a page, proceed as follows: When entering

'+'or <RETURN>: Representation of the next page
'-', 'b'or'B.': Representation of the previous page
'q'or'Q': Termination of the program

After the display of the last - possibly no longer complete - page, the program should not be terminated automatically (but only after entering 'q' or 'Q').

Note on the solution :

  • The characters controlling the output are to be read in with the - unfortunately not ANSI-C - function, so that the input of each character does not have to be terminated with .

    Example for the output of the DUMP program :

    Dumping File *** c: display.com *** (11053 bytes) Address 0 1 2 3 4 5 6 7 8 9 ABCDEF 0123456789ABCDEF 00000000: E9 51 29 90 90 CD AB 43 6F 70 79 72 69 67 68 74 .Q ) .... Copyright 00000010: 20 28 43 29 20 31 39 38 35 20 42 4F 52 4C 41 4E (C) 1985 BORLAN 00000020: 44 20 49 6E 63 02 08 00 20 54 00 1E 30 00 00 00 D Inc. .. T..0 ... 00000030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................00000040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000050: 00 00 00 00 00 0C 53 69 65 6D 65 6E 73 20 50 43 ...... Siemens PC 00000060: 2D 44 73 65 6C 65 63 74 65 64 50 19 00 04 1B 5B -DselectedP .... [00000070: 30 6D 00 00 00 00 00 00 00 00 00 00 00 00 04 1B 5B 0m ............. [00000080: 30 6D 00 00 00 00 00 00 00 00 00 00 00 00 08 1B 5B 0m ............. [00000090: 30 30 3B 30 30 48 30 48 00 00 00 00 00 00 01 01 00; 00H0H ........ 000000A0: 07 04 00 00 04 1B 5B 32 4A 00 03 1B 5B 66 00 00 .. .... [2J ... [f .. 000000B0: 04 1B 5B 31 4C 00 04 1B 5B 31 4D 00 00 00 03 1B .. [1L ... [1M ..... 000000C0: 5B 4B 00 00 04 1B 5B 30 6D 00 04 1B 5B 32 6D 00 [K .... [0m ... [2m. 000000D0: 00 00 08 1B 5B 32 35 3B 30 31 48 30 48 00 00 00 .... [25; 01H0H ... 000000E0: 00 00 2E 8A 27 0A E4 F9 74 0E 43 2E 8A 07 50 E8 ... .'... tC..P. 000000F0: 4C 05 58 FE CC 75 F3 F8 C3 8B D8 43 4B 75 01 C3 LX.u ..... CKu .. Address 0 1 2 3 4 5 6 7 8 9 ABCDEF 0123456789ABCDEF Continue with <+> or < RETURN> Back with <-> or Cancel with



Copyright © Munich University of Applied Sciences, FB 04, Prof. Dr. Rainer Thomas
U - PC - 021 - 01 - TH - 07
U - PC - 021 - 02 - TH - 07
U - PC - 022 - 00 - TH - 05
U - PC - 023 - 00 - TH - 03
U - PC - 024 - 01 - TH - 06
U - PC - 024 - 02 - TH - 07
U - PC - 025 - 00 - TH - 05
U - PC - 026 - 00 - TH - 07
U - PC - 027 - 00 - TH - 04
U - PC - 028 - 00 - TH - 04