Skip to content

Instantly share code, notes, and snippets.

@wise-introvert
Created December 13, 2021 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wise-introvert/ea64f0d54b47d618635981925a0b68ed to your computer and use it in GitHub Desktop.
Save wise-introvert/ea64f0d54b47d618635981925a0b68ed to your computer and use it in GitHub Desktop.
Debuggin Answer
----------------------------------------------BUGS----------------------------------------------
======== BUG 1 ========
Original Line: 13, 16
Issue: Case-sensitive comparision of characters
What was done: Used the "tolower" function from the ctype library to convert both the characters being compared to lowercase
Original Code:
    if(str[i] == s[0])
    if(str[i + j] == s[j])
Fixed Code:
    if(tolower(str[i]) == tolower(s[0]))
    if(tolower(str[i + j]) == tolower(s[j]))
======== BUG 2 ========
Original Line: 20
Issue: Wrong condition ( != ) was being used to determine if there was a valid match.
What was done: Used the correct condition (==)
Original Code:
    if(matched != len)
Fixed Code:
    if(matched == len)
======== BUG 3 ========
Original Line: 22
Issue: The index of the match ( "posn" ) was being assigned the wrong value ( i.e sum of the value of "i" and 1 )
What was done: Changed the value being assigned to the variable
Original Code:
    posn = i + 1;
Fixed Code:
    posn = i;
======== BUG 4 ========
Original Line: 35
Issue: The offset value was being decremented instead of being increased by the index of the match
What was done: Changed the arithmetic operator to the correct one ( "+" instead of "-" )
Original Code:
    offset -= lastPosn;
Fixed Code:
    offset += lastPosn;
----------------------------------------------FIXED CODE----------------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_POSITIONS 20
int findStr(const char str[],
  const char s[]) {
  int i, j, found = 0, matched = 0, posn = -1;
  int len = strlen(s);
  for (i = 0; str[i] != '\0' && !found; i++) {
    if (tolower(str[i]) == tolower(s[0])) {
      matched = 1;
      for (j = 1; str[i + j] != '\0' && s[j] != '\0'; j++) {
        if (tolower(str[i + j]) == tolower(s[j])) {
          matched++;
        }
      }
      if (matched == len) {
        found = 1;
        posn = i;
      }
    }
  }
  return posn;
}
int findAll(const char str[],
  const char s[], int positions[]) {
  int numFound = 0, lastPosn = 0, offset = 0;
  do {
    offset += lastPosn;
    lastPosn = findStr( & str[offset], s);
    if (lastPosn >= 0) {
      positions[numFound++] = offset + lastPosn;
      offset += strlen(s);
    }
  } while (lastPosn >= 0 && offset < strlen(str));
  return numFound;
}
int main(void) {
  char string1[] = {
    "The cat in the hat ate the mouse in the shoe"
  };
  char lookFor[] = {
    "the"
  };
  int foundIn[MAX_POSITIONS] = {
    0
  };
  int numberFound = 0, i;
  numberFound = findAll(string1, lookFor, foundIn);
  printf("%s found in positions: ", lookFor);
  for (i = 0; i < numberFound; i++) {
    printf("%d%s", foundIn[i + 1], (i == (numberFound - 1) ? "\n" : ", "));
  }
  return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment