Guest User

Untitled

a guest
Jun 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1.  
  2. /* The normalize procedure examines a character array of size len
  3.      in ONE PASS and does the following:
  4.      1) turn all upper case letters into lower case ones
  5.      2) turn any white-space character into a space character and,
  6.         shrink any n>1 consecutive spaces into exactly 1 space only
  7.             Hint: use C library function isspace()
  8.      You must do the normalization IN PLACE so that when the procedure
  9.      returns, the character array buf contains the normalized string and
  10.      the return value is the length of the normalized string.
  11. */
  12. int
  13. normalize(char *buf,    /* The character array containing the string to be normalized*/
  14.                     int len)            /* the size of the original character array */
  15. {
  16.     int i = 0;
  17.     int newLen = len;
  18.     for (i; i < newLen; i++) {
  19.         if (isupper(buf[i])) {
  20.             buf[i] = tolower(buf[i]);
  21.         }
  22.         if (isspace(buf[i])) {
  23.             buf[i] = 32;
  24.             while (isspace(buf[i+1]) || i == 0) { /* found a second space! */
  25.                 int j = i;
  26.                 while (j < newLen-1) {
  27.                     buf[j] = buf[j+1];/* push it to the end */
  28.                     j++;
  29.                 }
  30.                 newLen -= 1; /* cut it off. */
  31.             }
  32.             if (i == newLen-1) {
  33.                 printf("Removed the last space.\n");
  34.                 newLen -= 1;
  35.             }
  36.         }  
  37.     }
  38.     printf("%i \n", newLen);
  39.     return newLen;
  40. }
  41.    
  42. /* check if a query string ps (of length k) appears
  43.      in ts (of length n) as a substring
  44.      If so, return 1. Else return 0
  45.      You may want to use the library function strncmp
  46.      */
  47. int
  48. simple_match(const char *ps,    /* the query string */
  49.                          int k,                     /* the length of the query string */
  50.                          const char *ts,    /* the document string (Y) */
  51.                          int n) /* the length of the document Y */
  52. {
  53.     int counter = 0;
  54.     for (counter; counter < n; counter++) {
  55.         if (strncmp(&ts[counter],ps, k) == 0) {
  56.             printf("Found a match: %s | %s \n", &ps[0],&ps[1]);
  57.             return 1;
  58.         }
  59.     }
  60.     return 0;
  61. }
Add Comment
Please, Sign In to add comment