CS13002 Programming and Data Structures

Section 3/C, Spring 2003--2004

Assignment 4


Exercise 1

Input two strings a and b from the user and check if b is a substring of a. If b is a substring of a, then your program should also print the leftmost position of the leftmost match of b in a. You may use the fgets function (as exemplified here) to read a full line containing spaces.

Sample output

Enter the 1st string : ababbababbaa
Enter the 2nd string : abba
The 2nd string is a substring of the 1st, starting at position 2.

Enter the 1st string : This is a string.
Enter the 2nd string : s is a s
The 2nd string is a substring of the 1st, starting at position 3.

Enter the 1st string : This is yet another string!
Enter the 2nd string :
The 2nd string is a substring of the 1st, starting at position 0.

Enter the 1st string : This is the last example...
Enter the 2nd string : This is the example..
The 2nd string is not a substring of the 1st.

Test input

The first stringThe second string
abb acabcbabca abbacb bbca cbbabba
abb acabcbabca abbacb bbca cbbaacb
abb acabcbabca abbacb bbca cbbaa c b
abb acabcbabca abbacb bbca cbbaa cb
String!String!
String!!string!
A small string.<Empty string>
A small string. <Single space>
This is undoubtedly a bigger string!This is undoubtedly a big string!
This is undoubtedly a big string!This is undoubtedly a bigger string!


Exercise 2

Define a matrix by a two-dimensional array of integers. For simplicity work with square matrices of a fixed size (say, 10x10). Write a function that transposes such a matrix in place. Here `in place' means that the entry (i,j) is swapped with entry (j,i) in the two-dimensional array itself, that is, no additional matrix may be used.

You may populate your matrix by random entries, say, by using the rand() function call (as described here).

Sample output

Matrix before transposition :
 -92 -34 -55  -7  62   0  81  68  37 -36
  21  81 -23  66 -81  28 -44 -23 -50  87
  49 -47 -41  12 -59  32 -87 -55 -86  -4
  56 -78  61 -98  -9   0 -22 -28 -32 -85
  12  65  96  88  31  91  17  86 -32  43
  50  93  72  85   5 -87  17  -6 -66  31
  89  90  52  27  68 -80 -73 -54  -8 -29
 -62 -96 -87 -89  92  43  78 -14   6 -77
 -94 -44 -84  78 -58  20  67  35  91  78
 -34  80  68  94 -15  13 -85  11  58 -17

Matrix after transposition :
 -92  21  49  56  12  50  89 -62 -94 -34
 -34  81 -47 -78  65  93  90 -96 -44  80
 -55 -23 -41  61  96  72  52 -87 -84  68
  -7  66  12 -98  88  85  27 -89  78  94
  62 -81 -59  -9  31   5  68  92 -58 -15
   0  28  32   0  91 -87 -80  43  20  13
  81 -44 -87 -22  17  17 -73  78  67 -85
  68 -23 -55 -28  86  -6 -54 -14  35  11
  37 -50 -86 -32 -32 -66  -8   6  91  58
 -36  87  -4 -85  43  31 -29 -77  78 -17

Test input

Show two runs of your program. You may restrict the entries of your matrix between -999 and +999 (both inclusive).


Lab home