Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix the code and output should be 21 #include #include #define MAX_POSITIONS 32 int findStr(const char word[], const char* str) { int i, j, found

Fix the code and output should be 21

#include #include

#define MAX_POSITIONS 32

int findStr(const char word[], const char* str) { int i, j, found = 0, matched = 0, posn = -1; int len = strlen(word); for (i = 0; str[i] != '\0' && !found; i++) { if (str[i] == word[0]) { matched = 1; for (j = 1; str[i + j] != '\0' && word[j] != '\0'; j++) { if (str[i + j] == word[j]) { matched++; } } if (matched = len) { found = 1; posn = i; } } } return posn; }

int findMatches(const char word[], const char str[], int positions[]) { int lastPosn = 0, offset = 0, numFound = 0; do { offset += lastPosn; lastPosn += findStr(word, &str[offset]); if (lastPosn > -1) { positions[numFound++] = lastPosn - offset; offset += strlen(word); } } while (lastPosn >= 0 && offset < strlen(word)); return numFound; }

int main(void) { char phrase[] = { "I don't live day by day." }; char findWord[] = { "day" }; int foundIn[MAX_POSITIONS] = { 0 }; int matchesFound = 0, i; matchesFound = findMatches(findWord, phrase, foundIn); printf("%s found in positions: ", findWord); for (i = 0; i < matchesFound; i++) { printf("%d%s", foundIn[i], (i == (matchesFound - 1) ? " " : ", ")); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions