Question: Define a function named isSubstring that takes two C-strings s1 and s2 and returns true if s1 is a substring of s2; returns false otherwise.

Define a function named isSubstring that takes two C-strings s1 and s2 and returns true if s1 is a substring of s2; returns false otherwise. For example,

"abc" is a substring of "aaabc";

"you" is a substring of "how are you?";

"" (empty string) is a substring of any C-string;

"aaab" is a substring of "123aaaabcd";

"ab" is not a substring of "aacb";

"aaa" is not a substring of "aa";

just testing one case in the example, but the function is correct for all cases.

Do not use the substring function in the library to solve this problem as I have not learned.

Thank you so much.

This is my function code:

bool isSubstring(char array1[], char array2[]) { bool isSub = false; int length1 = 0; int length2 = 0; while (array1[length1] != '\0') length1++; while (array2[length2] != '\0') length2++; for (int i = 0; i <= length1 - length2; i++) { int j = 0; if (array2[j] == array1[i]) { for (; j < length2; j++) if (array2[j+1] == array1[i+1]) isSub = true; if (j == length2) return isSub; isSub = false; } } return isSub; }

I do not know what I am wrong. Can you help me to edit.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!