Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

0001 #include 0002 #include 0003 0004 #define MEX_POSITIONS 20 0005 0006 int findStr(const char str[], const char s[]) 0007 { 0008 int i, j, found

0001 #include 0002 #include 0003 0004 #define MEX_POSITIONS 20 0005 0006 int findStr(const char str[], const char s[]) 0007 { 0008 int i, j, found = 0, matched = 0, posn = -1; 0009 int len = strlen(s); 0010 //assume variable initialization is correct 0011 0012 for (i = 0; str[i] != '\0' && !found; i++) 0013 { 0014 if (str[i] == s[0]) 0015 { 0016 matched = 1; 0017 for (j = 1; str[i + j] != '\0' && s[j] != '\0'; j++) 0018 { 0019 if (str[i + j] == s[j]) 0020 { 0021 matched++; 0022 } 0023 } 0024 if (matched != len) 0025 { 0026 found = 1; 0027 posn = j; 0028 } 0029 } 0030 } 0031 0032 return posn; 0033 } 0034 0035 int findAll(const char str[], const char s[], int positions[]) 0036 { 0037 int numFound = 0, lastPosn = 0, offset = 0; 0038 //assume variable initialization is correct 0039 0040 do 0041 { 0042 offset += lastPosn; 0043 lastPosn = findStr(&str[offset], s); 0044 if (lastPosn <= 0) 0045 { 0046 positions[numFound++] = offset + lastPosn; 0047 offset += strlen(s); 0048 } 0049 } while (lastPosn >= 0 || offset < strlen(str)); 0050 return numFound; 0051 } 0052 0053 int main(void) 0054 { 0055 char string1[] = { "The cat in the hat ate the mouse in the shoe" }; 0056 char lookFor[] = { "the" }; 0057 int foundIn[MEX_POSITIONS] = { 0 }; 0058 int numberFound = 0, i; 0059 //assume there are no errors from the start of main to this comment 0060 0061 numberFound = findAll(string1, lookFor, foundIn); 0062 printf("'%s found in positions: ", lookFor); 0063 for (i = 0; i < numberFound; i++) 0064 { 0065 printf("%d%s", foundIn[i], (i == (numberFound - 1) ? " " : ", ")); 0066 } 0067 return 0; 0068 }

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

13. What is a capital account deficit?

Answered: 1 week ago

Question

How many lobes are in the cerebral cortex?

Answered: 1 week ago

Question

The central nervous system?

Answered: 1 week ago

Question

How many neurones does the human brain contain?

Answered: 1 week ago

Question

Brain part of logical and dicision making......?

Answered: 1 week ago