Starting from:

$30.99

CS217-Object Oriented Programming: Assignment I Solved

Q1: String manipulation functions
Write the implementation of the following functions:

int          Strlen ( char ∗s1 )

/∗Returns the length                    of

{

}
the         string
in number
of
characters . ∗/
 
 
 
 
 
char ∗Strcpy ( char ∗s1 ,
const char
∗s2 )
 
 
/∗Copies         string     s2         into

{

}
array s1 .
The value
of
s1     is returned . ∗/
1

2

3

4

1

2

3

4

1 char ∗Strncpy ( char ∗s1 , const char ∗s2 , size t n ) 2 /∗Copies at most n characters of string s2 into array s1 .

3       The value of    s1    is returned . ∗/

{

1              char ∗StrCat ( char ∗s1 ,   const char ∗s2 ) 2 /∗Appends string               s2 to array s1 .

3 The f i r s t character of s2 overwrites 4 the terminating null character of s1 .

5       The value of    s1    is returned . ∗/
{ 1 char ∗StrnCat( char ∗s1 , const char ∗s2 , size t n ) 2 /∗Appends at most n characters of string s2 to array s1 .

3   The f i r s t  character of   s2 overwrites the terminating 4  null characters of            s1. The 5 value of s1 is returned. ∗/
{

1 int StrCmp( const char ∗s1 , const char ∗s2 ) 2 /∗Compares the string s1 with the string s2 .

3  The function returns   0, less than 0, or greater 4  than 0    if  s1  is equal to,               less than or greater 5  than s2,  respectively. ∗/
{

1              int StrnCmp( const char ∗s1 ,                  const char ∗s2 ,             size t n )

2   /∗Compares up to n characters of the string 3 s1 with the string     s2. The function returns   0, 4 less than 0 or greater than 0 If            s1            is equal 5  to,  less than or greater than s2, respectively. ∗/
{

char ∗∗StrTok( char ∗s1 , const char s2 ) /∗A call to StrTok breaks string s1 into
' ' tokens ' '            ( logical        pieces          such
as words
in a line of            text ) separated by contained             in char s2∗/

{
character
int          StrFind ( char ∗s1 ,                char ∗s2 )

/∗Searches the              string      s1    for      the
f i r s t          occurrence
of     the      string          s2 and returns           its

i f              s2 not found returns −1.∗/

{
starting             index,
1

2

3

4

5

1

2

3

4

1             char ∗ SubStr ( char ∗ ,              int pos ,       int     len )

2          /∗This function  returns a newly constructed
3 string variable with its value initialized 4 to a copy of a substring of this variable. 5 The substring is the portion of the string

6  that starts at character position ' ' pos ' ' and 7      spans     ' ' len ' '  characters    ( or until the end 8   of the string,   whichever comes f i r s t ). ∗/
{

Figure 1: A mapping table between English Alphabets and Morse codes. Source: http://en.wikipedia.org/ wiki/Morse code

Q2: Morse Code
Morse code has been one of the most basic communication protocol and still used to convey SOS messages and other urgent communication. In Morse code each English alphabet is encoded by a sequence of ’.’ and ’-’, see Figure 1 for complete mapping between English alphabets and Morse codes. Letters are separated by spaces and words by ”/”. Your task is to design a program that can (i)convert any given string into a Morse code sequence char* convertToMorseCode(char*) and (ii) a Morse code sequence to a string. char* convertToString(char*) You are not authorized to use string data type, however you can use char*

Q3: Magic Squares
In this question, your goal is to write code for solving magic square (for details read the attached file magicsquare.pdf) using 2-D pointers. Your function should take the dimension of the magic square as input and then solve the magic square for a given dimension. You are required to return a 2d pointer to an integer.

1            int ∗∗ magic square ( int           size )

2              /∗This function returns a 2d pointer that dynamically 3  allocates a matrix of size passed as arguments 4 and store the magic square in allocated matrix . ∗/
{

Q4: Text Analysis
The availability of computers with string-manipulation capabilities has resulted in some rather interesting approaches to analyzing the writings of great authors. This exercise examines three methods for analyzing texts with a computer. You have to use char * for the following exercises.

Write a function that receives a string consisting of several lines of text and returns an array indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase To be, or not to be: is the question: that contains one a, two bs, no cs, and so on.
1 void count letters ( char ∗string , int ∗&array , int & size ) 2 /∗Parameters :

Input :
char ∗ : a multiline string 5 Output :
6   int ∗: an array containing   counts    of each   letter, 7 to be allocated     in            function 8   int:  array      size         ∗/
{

Write a function that receives a string consisting of several lines of text and returns an array indicating the number of one-letter words, two-letter words, three-letter words, and so on, appearing in the text. For example, the phrase Whether this nobler in the mind to suffer contains 2, 3, 4, etc. length words.
1 void countWordsBasedOnLength( char ∗string , int ∗&array/∗to be allocated ∗/ , 2 int & size /∗updated array size ∗/) 3 /∗Parameters :

Input :
char ∗ : a multi−line string 6 Output :
7   int  ∗: an array containing   counts    of each   different length words, 8  to be allocated     in     function 9  int:     array      size         ∗/
{

Write a function that receives a string consisting of several lines of text and returns arrays indicating unique words and the number of occurrences of each unique word in the text along with their size.
void counting unique words( char ∗string , char ∗∗&uwords/∗ l i s t   of unique words ; ∗/ ,
int ∗&array/∗to be allocated ∗/ , int & size /∗updated array size ∗/) 3 /∗Parameters :
Input :
char ∗ : a multiline string 6 Output :

More products