CS13002 Programming and Data Structures

Section 3/C, Spring 2003--2004

Assignment 4 : Solution


Exercise 1

#include <stdio.h>

#define MAXLEN 100

int issubstr ( char a[], char b[] )
{
   int alen, blen, i, j, match;

   alen = strlen(a);
   blen = strlen(b);
   for (i=0; i<=(alen-blen); ++i) {
      match = 1;
      for (j=0; (j<blen)&&(match); ++j) {
         if (a[i+j]!=b[j]) match = 0;
      }
      if (match) return(i);
   }
   return(-1);
}

int main ()
{
   char a[MAXLEN], b[MAXLEN];
   int l;

   printf("Enter the 1st string : "); fgets(a,MAXLEN,stdin);
   l = strlen(a) - 1; a[l] = 0;
   printf("Enter the 2nd string : "); fgets(b,MAXLEN,stdin);
   l = strlen(b) - 1; b[l] = 0;
   l = issubstr(a,b);
   if (l < 0)
      printf("The 2nd string is not a substring of the 1st.\n");
   else
      printf("The 2nd string is a substring of the 1st, starting at position %d.\n",l);
}

Output

Enter the 1st string : abb acabcbabca abbacb bbca cbba
Enter the 2nd string : bba
The 2nd string is a substring of the 1st, starting at position 16.

Enter the 1st string : abb acabcbabca abbacb bbca cbba
Enter the 2nd string : acb
The 2nd string is a substring of the 1st, starting at position 18.

Enter the 1st string : abb acabcbabca abbacb bbca cbba
Enter the 2nd string : a c b
The 2nd string is not a substring of the 1st.

Enter the 1st string : abb acabcbabca abbacb bbca cbba
Enter the 2nd string : a cb
The 2nd string is a substring of the 1st, starting at position 25.

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

Enter the 1st string : String!!
Enter the 2nd string : string!
The 2nd string is not a substring of the 1st.

Enter the 1st string : A small string.
Enter the 2nd string :
The 2nd string is a substring of the 1st, starting at position 0.

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

Enter the 1st string : This is undoubtedly a bigger string!
Enter the 2nd string : This is undoubtedly a big string!
The 2nd string is not a substring of the 1st.

Enter the 1st string : This is undoubtedly a big string!
Enter the 2nd string : This is undoubtedly a bigger string!
The 2nd string is not a substring of the 1st.


Exercise 2

#include <stdio.h>

#define MATDIM 10

int mat[MATDIM][MATDIM];

void printMatrix ( int mat[MATDIM][MATDIM] )
{
   int i, j;

   for (i=0; i<MATDIM; ++i) {
      for (j=0; j<MATDIM; ++j) {
         printf("%5d",mat[i][j]);
      }
      printf("\n");
   }
   printf("\n");
}

void transposeMatrix ( int mat[MATDIM][MATDIM] )
{
   int i, j, t;

   for (i=0; i<MATDIM; ++i) {
      for (j=i+1; j<MATDIM; ++j) {
         t = mat[i][j];
         mat[i][j] = mat[j][i];
         mat[j][i] = t;
      }
   }
}

int main ()
{
   int i, j;

   srand((unsigned int)time(NULL));
   for (i=0; i<MATDIM; ++i) for (j=0; j<MATDIM; ++j)
      mat[i][j] = rand() % 1999 - 999;
   printf("Matrix before transposition :\n");
   printMatrix(mat);
   transposeMatrix(mat);
   printf("Matrix after transposition :\n");
   printMatrix(mat);
}

Output

Matrix before transposition :
 -887 -648  798 -922  923  349 -819 -848  342 -495
 -925   38  286 -453  418  190 -517  -70  745  635
  694 -705 -153 -408  708 -865  563  647  508 -878
 -154  620 -454 -356  770 -531 -934 -976 -307  407
 -398 -233 -482  887  313  936  151  795 -134 -104
  504 -367  263 -649  225  -29 -442 -212 -382 -861
  -18  536  758  601 -820  601 -930 -682  698 -164
  798 -700  602 -684 -813  -11 -748  337 -143  190
 -694 -639  896  568 -216  121 -461  414  981  229
  552   36 -235  383 -363   17   58 -293  335 -244

Matrix after transposition :
 -887 -925  694 -154 -398  504  -18  798 -694  552
 -648   38 -705  620 -233 -367  536 -700 -639   36
  798  286 -153 -454 -482  263  758  602  896 -235
 -922 -453 -408 -356  887 -649  601 -684  568  383
  923  418  708  770  313  225 -820 -813 -216 -363
  349  190 -865 -531  936  -29  601  -11  121   17
 -819 -517  563 -934  151 -442 -930 -748 -461   58
 -848  -70  647 -976  795 -212 -682  337  414 -293
  342  745  508 -307 -134 -382  698 -143  981  335
 -495  635 -878  407 -104 -861 -164  190  229 -244


Matrix before transposition :
 -773  338 -652  429  522  867 -629  143  464 -998
 -400 -465 -756 -886 -796  629  818   14 -270  132
 -825  341 -670 -730 -127  704 -148  179   57  813
 -844  283  151 -497 -288 -254 -557  155  962  907
  156 -438 -485  400 -252 -281   29 -361  732  759
  771  980  173 -826 -750 -881  877  102  371  -65
  -12  526 -709 -861 -898   76  -43 -456 -769  -81
 -476  387  554   39 -140 -625  830  888   14  635
  720 -142  615 -106  104  938   85   54  113 -544
 -938 -899 -945 -647 -688 -844  428  268 -300  731

Matrix after transposition :
 -773 -400 -825 -844  156  771  -12 -476  720 -938
  338 -465  341  283 -438  980  526  387 -142 -899
 -652 -756 -670  151 -485  173 -709  554  615 -945
  429 -886 -730 -497  400 -826 -861   39 -106 -647
  522 -796 -127 -288 -252 -750 -898 -140  104 -688
  867  629  704 -254 -281 -881   76 -625  938 -844
 -629  818 -148 -557   29  877  -43  830   85  428
  143   14  179  155 -361  102 -456  888   54  268
  464 -270   57  962  732  371 -769   14  113 -300
 -998  132  813  907  759  -65  -81  635 -544  731


Lab home