Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The normalize procedure examines a character array of size len
- in ONE PASS and does the following:
- 1) turn all upper case letters into lower case ones
- 2) turn any white-space character into a space character and,
- shrink any n>1 consecutive spaces into exactly 1 space only
- Hint: use C library function isspace()
- You must do the normalization IN PLACE so that when the procedure
- returns, the character array buf contains the normalized string and
- the return value is the length of the normalized string.
- */
- int
- normalize(char *buf, /* The character array containing the string to be normalized*/
- int len) /* the size of the original character array */
- {
- int i = 0;
- int newLen = len;
- for (i; i < newLen; i++) {
- if (isupper(buf[i])) {
- buf[i] = tolower(buf[i]);
- }
- if (isspace(buf[i])) {
- buf[i] = 32;
- while (isspace(buf[i+1]) || i == 0) { /* found a second space! */
- int j = i;
- while (j < newLen-1) {
- buf[j] = buf[j+1];/* push it to the end */
- j++;
- }
- newLen -= 1; /* cut it off. */
- }
- if (i == newLen-1) {
- printf("Removed the last space.\n");
- newLen -= 1;
- }
- }
- }
- printf("%i \n", newLen);
- return newLen;
- }
- /* check if a query string ps (of length k) appears
- in ts (of length n) as a substring
- If so, return 1. Else return 0
- You may want to use the library function strncmp
- */
- int
- simple_match(const char *ps, /* the query string */
- int k, /* the length of the query string */
- const char *ts, /* the document string (Y) */
- int n) /* the length of the document Y */
- {
- int counter = 0;
- for (counter; counter < n; counter++) {
- if (strncmp(&ts[counter],ps, k) == 0) {
- printf("Found a match: %s | %s \n", &ps[0],&ps[1]);
- return 1;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment