Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2024

 

Tasks | Task groups | Text

PrevNext


Text files

The condition "A text file is given" means that the file name is given (as a string input data) and the corresponding file exists and is located in the working directory. File components (as a rule, lines of characters) must be read using standard input procedures (or functions) of a programming language being used.

If the task requires to create a new file then the new file name is included in the input data set; this name is usually the last element of input data. Standard output procedures (or functions) of the programming language must be used for writing data to files.

The size of any input file is not fixed, so additional arrays should not be used to store all file components but one may use temporary files.

Binary files may also be used in the tasks of "Text files" group; see the beginning of the section "Binary files" for additional rules connected with files of this kind.

Text files: basic operations

Text1°. Given a file name and two positive integers N and K, create a new text file with this name. The file must contain N lines, each line consists of K characters "*".

Text2. Given a file name and an integer N (0 < N < 27), create a new text file with this name. The file must contain N lines, the first line consisting of one Latin small letter "a", the second one consisting of two letters "ab", and so on; the last line must consist of N initial small letters of the English alphabet.

Text3. Given a file name and an integer N (0 < N < 27), create a new text file with this name. The file must contain N lines of length N. The K-th line (K = 1, …, N) must begin with K initial capital letters of the English alphabet and must end with the characters "*". For instance, if N equals 4 then lines of the resulting file must be as follows: "A***", "AB**", "ABC*", "ABCD".

Text4°. Given a text file, output the amount of its characters and lines. EOLN (end-of-line) and EOF (end-of-file) markers must not be counted.

Text5. Given a string S and a text file, add the string S to the end of the file.

Text6. Given two text files, add the contents of the second file to the end of the first one.

Text7. Given a string S and a text file, add the string S to the beginning of the file.

Text8. Given two text files, add the contents of the second file to the beginning of the first one.

Text9. Given an integer K and a text file, insert an empty line before the file line with the order number K. If the file does not contain a line with the required number then do not change it.

Text10. Given an integer K and a text file, insert an empty line after the file line with the order number K. If the file does not contain a line with the required number then do not change it.

Text11. Given a text file, double occurrences of all empty lines of the file.

Text12. Given a string S and a text file, replace all empty lines of the file with the contents of the string S.

Text13. Given a nonempty text file, remove its first line.

Text14. Given a nonempty text file, remove its last line.

Text15. Given an integer K and a text file, remove the file line with the order number K. If the file does not contain a line with the required number then do not change it.

Text16°. Given a text file, remove all empty lines from the file.

Text17. Given two text files, add each line of the second file to the end of the corresponding line of the first one. If the second file is shorter (in lines) than the first one then do not change the remaining lines of the first file.

Text18. Given an integer K and a text file, remove K leading characters from each line of the file. If the length of some line is less than K then remove all characters from the line.

Text19. Given a text file, replace all its Latin capital letters with the corresponding small letters, and all Latin small letters with the capital ones.

Text20. Given a text file, replace its successive blank characters with the single blank character.

Text21°. Given a text file that contains more than three lines, remove its last three lines.

Text22. Given an integer K (0 < K < 10) and a text file that contains more than K lines, remove its last K lines.

Text23. An integer K (0 < K < 10) and a text file that contains more than K lines are given. Create a new file that contains K last lines of the given file.

Text analysis and formatting

Text24°. A text file is given. Find the amount of paragraphs of the given text provided that paragraphs are separated by one or more empty lines.

Text25. An integer K and a text file are given. Remove a paragraph with the order number K from the given file provided that paragraphs are separated by one or more empty lines. Empty lines must not be removed. If the file does not contain a paragraph with the required number then do not change it.

Text26. A text file is given. Find the amount of paragraphs of the given text provided that the first line of each paragraph is indented by five blank characters. Empty lines must not be counted.

Text27. An integer K and a text file are given. Remove a paragraph with the order number K from the given file provided that the first line of each paragraph is indented by five blank characters. Empty lines must not be counted and removed. If the file does not contain a paragraph with the required number then do not change it.

Text28. A text file is given. The file does not contain empty lines, the first line of each paragraph is indented by five blank characters. Insert an empty line between adjacent paragraphs (do not insert empty lines to the beginning and end of the file).

Text29. A text file is given. Output its first word of the maximal length. A word is defined as a character sequence that does not contain blank characters and is bounded by blank characters or the line beginning/end.

Text30. A text file is given. Output its last word of the minimal length. A word is defined as a character sequence that does not contain blank characters and is bounded by blank characters or the line beginning/end.

Text31. An integer K and a text file are given. Create a new binary file of strings that contains all words of length K from the given file. A word is defined as a character sequence that does not contain blank characters, punctuation marks and is bounded by blank characters, punctuation marks or the line beginning/end. If the given file does not contain words of length K then the resulting file must be empty.

Text32. A char C that is a Latin capital letter and a text file are given. Create a new binary file of strings that contains words of the given text with the first character C (of uppercase or lowercase). A word is defined as a character sequence that does not contain blank characters, punctuation marks and is bounded by blank characters, punctuation marks or the line beginning/end. If the given file does not contain the required words then the resulting file must be empty.

Text33. A char C that is a Latin small letter and a text file are given. Create a new binary file of strings that contains words of the given text with at least one character C (of uppercase or lowercase). A word is defined as a character sequence that does not contain blank characters, punctuation marks and is bounded by blank characters, punctuation marks or the line beginning/end. If the given file does not contain the required words then the resulting file must be empty.

Text34°. A text file whose lines are left-aligned is given. Make the given text right-aligned by means of adding leading blank characters to all nonempty lines. The width of text must be equal to 50 characters.

Text35. A text file whose lines are left-aligned is given. Make the given text centered by means of adding leading blank characters to all nonempty lines. The width of text must be equal to 50 characters. If the length of line is an odd number then add one blank character to the beginning of this line before centering.

Text36. A text file whose lines are right-aligned is given. Make the given text centered by means of removing a half of leading blank characters from all nonempty lines. If the amount of leading blank characters of line is an odd number then remove one blank character from the beginning of this line before centering.

Text37. A text file whose lines are left-aligned is given. Paragraphs of the given text are separated by one empty line. Make the given text full-aligned (that is, left-aligned and right-aligned simultaneously) by means of inserting additional blank characters between words in all nonempty lines except the last line of each paragraph. Spaces between words in each line must be processed from right to left; the width of text must be equal to 50 characters.

Text38°. An integer K (> 25) and a text file, whose lines are left-aligned, are given. Paragraphs of the given text are separated by one empty line. Format the given text so that all its lines consist of no more than K characters. The resulting text must contain the same paragraphs and be left-aligned. Blank characters at the end of lines must be removed. Save the formatted text in a new text file.

Text39. An integer K (> 25) and a text file, whose lines are left-aligned, are given. The file does not contain empty lines, the first line of each paragraph is indented by five blank characters. Format the given text so that all its lines consist of no more than K characters. The resulting text must contain the same paragraphs and be left-aligned. Blank characters at the end of lines must be removed. Save the formatted text in a new text file.

Text files with numeric data

The decimal separator in string representation of any real number with nonzero fractional part is assumed to be the decimal point "." in all tasks of this subsection.

Text40. Two files of integers are given; the files contain the equal amount of components. Create a new text file that contains string representations of components of the given binary files. These representations must be arranged in two columns, each of 30 characters width (the first and second columns contain components of the first and second file respectively). Each line of the resulting text must start and end with "|" separator (the numeric value 124), all string representations of integers are right-aligned with respect to column boundary.

Text41. Three files of integers are given; the files contain the equal amount of components. Create a new text file that contains string representations of components of the given binary files. These representations must be arranged in three columns, each of 20 characters width (the first, second, and third columns contain components of the corresponding file). Each line of the resulting text must start and end with "|" separator (the numeric value 124), all string representations of integers are left-aligned with respect to column boundary.

Text42°. Real numbers A, B and an integer N are given. Create a text file that contains table of values of the function (x)1/2 at points of the segment [AB] with the step H = (B − A)/N (that is, at the points A, A + H, A + 2·H, …, B). The table consists of two columns, the first column contains arguments x, the second one contains the function values (x)1/2. The width of the columns is 10 and 15 characters respectively; the width of the fractional part of arguments and function values is 4 and 8 respectively; all string representations of numbers are right-aligned with respect to column boundary.

Text43. Real numbers A, B and an integer N are given. Create a text file that contains table of values of the functions sin(x) and cos(x) at points of the segment [AB] with the step H = (B − A)/N (that is, at the points A, A + H, A + 2·H, …, B). The table consists of three columns, the first column contains arguments x, the second and the third one contains the function values sin(x) and cos(x) respectively. The width of the first column is 8 characters, the width of the other columns is 12 characters; the width of the fractional part of arguments and function values is 4 and 8 respectively; all string representations of numbers are right-aligned with respect to column boundary.

Text44°. A text file is given. Each line of the file represents an integer padded by several leading and trailing blank characters. Output the amount and sum of these integers.

Text45. A text file is given. Each line of the file represents an integer or real number padded by several leading and trailing blank characters. All real numbers have nonzero fractional part. Output the amount and sum of numbers with nonzero fractional part.

Text46. A text file is given. Each line of the file represents several integers and real numbers that are separated by one or more blank characters. All real numbers have nonzero fractional part. Create a binary file of real numbers that contains all given numbers with nonzero fractional part (in the same order).

Text47. A text file is given. Each line of the file represents an integer or real number padded by several leading and trailing blank characters. All real numbers have nonzero fractional part. Output the amount and sum of integers.

Text48. A text file is given. Each line of the file represents several integers and real numbers that are separated by one or more blank characters. All real numbers have nonzero fractional part. Create a binary file of integers that contains all integers (in the same order).

Text49. A text file and a binary file of integers are given. Add a string representation of each integer from the binary file to the end of the corresponding line of the text file. If the amount of integers is less than the amount of text lines then do not change remaining text lines.

Text50. A text file is given. Each line of the text file consists of a text string (of 30 characters length) and a representation of a real number. Create two binary files; the first one is a file of strings that contains text parts from the given text file, the second one is a file of real numbers that contains numbers from the given text file (in the same order).

Text51. A text file is given. The file contains a table of real numbers; the table consists of three columns. The width of each column and the alignment of numbers are arbitrary. Create three binary files of real numbers; each file must contain numbers from the corresponding table column (in the same order).

Text52. A text file is given. The file contains a table of real numbers; the table consists of three columns. A character-separator is placed between adjacent columns, before the first column and after the last one. The width of each column and the alignment of numbers are arbitrary; a character that is used as a separator is arbitrary too. Create a binary file of integers that contains the sum of numbers from each row of the table.

Text files: additional tasks

Text53. Given a text file, create a new binary file of characters that contains all punctuation marks of the text (in the same order).

Text54. A text file is given. Create a new binary file of characters that contains all characters of the given text (without repetitions) including blank character and punctuation marks. The order of characters is determined by their first occurrence in the text.

Text55. A text file is given. Create a new binary file of characters that contains all characters of the given text (without repetitions) including blank character and punctuation marks. The characters must be in ascending order of their numeric values in the character set.

Text56. A text file is given. Create a new binary file of characters that contains all characters of the given text (without repetitions) including blank character and punctuation marks. The characters must be in descending order of their numeric values in the character set.

Text57°. A text file is given. Find the amount of occurrences of each Latin small letter and create a new binary file of strings whose components have the following format: "<a letter>–<amount of its occurrences>" (for example, "a–25"). Letters that are absent in the text should not be included in the new file. Strings of the resulting file must be in alphabetic order.

Text58. A text file is given. Find the amount of occurrences of each Latin small letter and create a new binary file of strings whose components have the following format: "<a letter>–<amount of its occurrences>" (for example, "a–25"). Letters that are absent in the text should not be included in the new file. Strings of the new file must be in descending order of the amount of letter occurrences; if some letters have the equal amount of occurrences then the corresponding strings must be in alphabetic order.

Text59. A string S that consists of 10 digits and a text file that contains an English text are given. Encrypt the file using the right cyclic shift of any letter by SK mod 10 + 1 positions of the English alphabet, where K is the line position of the letter, characters SN of the given string S are numbered beginning with 1, and "mod" denotes the operator of taking the remainder after integer division. For instance, letters that are placed in the line positions 1, 11, 21, … are shifted by S1 positions of alphabet, letters that are placed in the line positions 2, 12, 22, … are shifted by S2 positions of alphabet, and so on). Do not change blank characters and punctuation marks.

Text60. A string and a text file are given. The file contains an encrypted English text (the method of encryption is described in Text59), the string is the first decrypted line of the given text. Decrypt the file; if the given information is insufficient for decryption then do not change the given file.


PrevNext

 

  Ðåéòèíã@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
01.01.2024