Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



Department of Engineering Mathematics and Internetworking

ENGM1081:  Computer Programming

 

ASSIGNMENT # 10

For these questions you are not allowed to use the string functions in string.h

1. Write a program which declares a string and sets it to contain the phrase “Egalitarian delegates allegedly forego eggs”.  The string is first written to an output file and is then passed to a function called convert() which converts each occurrence of “eg” to “EG”. The main program then writes the converted string to the output file. Your program should consist of both main

and the function convert().

The output file should look like:

The   o r i g i n a l   s t r i n g   i s :

E g a l i t a r i a n   d e l e g a t e s   a l l e g e d l y   f o r e g o   e g g s

The   s t r i n g   now   i s :

E g a l i t a r i a n   delEGates   all EGedly   forEGo  EGgs

2. Write a function called concat() which takes two strings s1 and s2 as arguments and sets up a third string, s3, in which the second string is concatenated (i.e.  added) to the end of the first string.  In other words, the third string consists of all the characters of s1 up to but not including the null character followed by all the characters in s2 including the final null character. For example, if s1 is “agree” and s2 is “able”, then s3 would be “agreeable”. The function concat() should also return the length of s3.  Include a main program which tests your function.

For example, the program output might look like:

The  c o n c a t e n a t i o n   o f   the   s t r i n g :   ”a g r e e ”

and  the   s t r i n g :   ”a b l e ”

i s   the   s t r i n g :   ”a g r e e a b l e ”

which   i s   9   c h a r a c t e r s   i n   l e n g t h .

3. Write two functions, first() and last(), described as follows:

❼ the function first() takes a string s and a character c as arguments and returns a pointer

to the first occurrence of the character c in the string s.  That is, if s =  "A  string to be  searched" and c =  ‘e’, then the function first() returns a character pointer to the ‘e’in the word "be". If the character does not occur in the string s, then the value NULL is returned.

❼ the function last() takes a string s and a character c as arguments and returns a pointer

to the last occurrence of the character c in the string s.  That is, if s =  "A  string to be  searched" and c =  ‘e’, then the function last() returns a character pointer to the ‘e’ just before the  ‘d’in the last word.  Again, if the character does not occur in the string, then the NULL pointer is returned.

Write a main program which defines a string, str, which contains the sentence “A string to be searched”. Your program should then prompt the user for the character to search for, c. The functions first() and last() are then called with str and c as arguments and the resulting strings (starting at the returned pointers) are printed to an output file.  For example, if you type in the letter ‘s’ as the character to search for, your program should print out:

O r i g i n a l   s t r i n g :

A  s t r i n g   to   be   s e a r c h e d

F i r s t   o c c u r r e n c e   o f   ‘ s ’   s t a r t s   the   s t r i n g :

s t r i n g   to   be   s e a r c h e d

Last   o c c u r r e n c e   o f   ‘ s ’   s t a r t s   the   s t r i n g :

s e a r c h e d

Test your program using the characters  ‘r’, ‘e’and ‘q’. Note that the last should give the error message: Character  ‘q’ not  found.


 

4. Write a function, changename(), which takes a filename as input and produces a filename with a different extension as output in a second string. That is, the input filename string is copied to the output filename string, except that the extension on the latter is changed. The desired new extension should be provided as a third string to the function.

Most filenames consist of two parts, the basename (everything to the left of the last ‘.’  char- acter), and the extension (everything to the right of the last ‘.’  character).  Often, we wish to name our output files something similar to our input files, and changing the extension is a convenient way to do this.  For example, if the input file is called  "myprog.in ", then an appropriate output file name would be "myprog.out".

Write a main program to first read a string (with no spaces in it) from the keyboard.  This is the ‘input’ filename (although we will not actually open this file).  The main function then calls changename() to create the ‘output’ string which, as explained above, is a copy of the input string except that everything after the last ‘.’  character is replaced with the extension ‘out’.  Hint:  you can use the function last() that you wrote in the previous question to help with this.  The input string and the output string are written to a file having the modified filename.

For example, if the filename  "my.program.in " is entered, the output file my.program.out should contain (and you should test your program using an input filename having more than one ‘.’ character in it, as shown here):

i n p u t  name   i s  my . program . i n

output  name   i s  my . program . out

5. In some programs we would like to be able to determine the input and output filenames at the time that the program is executed. Modify your maxmin.c program from Assignment 8 so that the main program prompts the user for the two filenames and passes the input filename to the readvalues() function.  The function readvalues() now opens the input file to read the data and closes it when done.

This program uses a second function, range(), to find the maximum and minimum of a set of floats stored in the input file.

Your main must use the following steps:

❼ prompt user for the input and output file names.

❼ call the function readvalues() to open the input file, read the values from the intput

file into the array x, and return the value of n (number of values in the file).

❼ call the function range() which computes the maximum and minimum values found in

the array x.

❼ open the output file to write out the results of the run  (the maximum and minimum

values).

The program should interact with the user as follows:

Enter   the   i n p u t   f i l e n a m e   :   s a m p l e s i n . t x t

Enter   the   output   f i l e n a m e :   s a m p l e s o u t . t x t

Use the same input file, samplesin.txt, as in Assignment 8. Then your output file should be the same as for Assignment 8, maxminout.txt.