Starting from:

$30

CSE 1242 - COMPUTER PROGRAMMING II Programming Assignment # 2 Solved   



 

 

In this homework, you will implement your own String data type using C structs. Each String  is represented by a sequence of characters and should have a length property that shows the number of characters stored in that string (excluding the null character). Suppose that this struct can be used to represent only the strings with less than 50 characters. You should implement the following functions to operate on your String data type. 

 

•       int charAt(String *s, int index) 

o This function should return the character stored at the location index. If there is no such an index (either negative index value, or greater or equal to the length of the string), then return -1. 

 

•       String *concat(String *s1, String *s2) 

o This function should concatenate the string pointed by s2 to the end of the string pointed by s1, and return the address of the new string. You cannot use strcat or strncat functions of string.h library of C in that function. 

 

•       unsigned int strSearch(String *s1, String *s2) 

o This function should scan the first string and return the length of initial segments that contain only the characters of the second string. If there is no such a segment, return -1. You cannot use strspn or strcspn functions of string.h library of C in that function. 

 

•       unsigned int findScore(String *s1) 

o This function should return a score by checking the given String. To calculate the score, you will use the following table: 

 

Letter                           Value 

A, E, I, O, U, L, N, R, S, T       1 

D, G                               2 

B, C, M, P                         3 

F, H, V, W, Y                      4 

K                                  5 

J, X                               8 

Q, Z                               10 

 

o For example, the String is “hello” should be scored as 4+1+2∗1+1=8.  

 

 

•       Your program should continue to execute until the given word is either “exit” or “quit”. If the given word is “exit” or “quit”, your program should terminate. If the given word is “stat”, your program should print a two-line statistical report: the first line prints the total number of words the user has entered, and the second line prints the total number of alphabetic letters the user has entered. Note that we only count the 26 letters in the alphabet (either lower case or upper case). For example, if a user has entered the following string, i.e. “We love CSE1142 course a lot!!”, the following should be printed to screen after the user types “stat”: 

 

The number of words: 6 

The number of alphabetic letters: 19 

 

•       If the given word is not “stat”, “exit” or “quit”, then your program will continue to execute based on the user’s selection.  

 

•       You have to perform File I/O operations in this homework. 

o   All the user options will be given using an input file that is passed to your program using command line arguments. 

o   All the outputs of your program must be printed to an output file that is passed to your program using command line arguments. 

o   For example, suppose that “input.txt” and “output.txt” are given to your program as command line arguments. 

o   Each line of the input file contains the related operations followed by the specified options. 

o   For example, if a line is as follows: 

  

heLLo world:1,6 

 

Here, o “heLLo world” is the given string that we use.  o 1 represents that the operation is finding a char o 6 represents the index for the searched char. 

o   The output for this option should be W, which will be added to a given output file.  

•       The valid options given as the second argument (given after colon : ) can be o 1 for finding a char in the given index, o 2 for string concatenation, o 3 for string search,  o 4 for finding the string score. 

 

•       If the required operation needs a third option (given after comma , ) it can be o An index for finding a char in the given string, o A second string for string concatenation, o A second string for string search. 

 

•       An operation may not require a third option (For example, finding the string score), in such a case, you can directly find the score of the string. 

 

•       Suppose that the input file contains the following lines: 

 

heLLo world:1,6 

Welcome to C:2,programming! 

welcome to marmara university!:3,mar We love CSE!!:4 stat exit   

•       Then, the output file should contain the output of each operation as follows: 

  



Welcome to C programming! 



17  

The number of words: 14 

The number of alphabetic letters: 70 

Program ends. Bye 

 

 

 


More products