Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include int strlength(char str[], int length, int *first, int *last) { char curr_char = str[0]; int start=0;; int finish=0; int curr_start=0; int max_length =
#includeProblem 4 [1 pt]. This problem is a coding problem, unrelated to probability, but good practice for algorithm design. Attached is a program strlength.c, and it has a function int strlength(char str[ ], int length, int * first, int *last) /* 'length' is the length of string str[ ]) */ that - Returns the length of the longest substring of str[ ] of a single character - Sets "first" and "last" as the indices where the substring starts and ends For example, aaabaabbababbbba has the longest substring bbbb which has length 4, and starts at position 11 and ends at position 14. Modify the function strlength(), so that it finds the longest substring with at most two distinct characters. For example#include int strlength(char str[], int length, int *first, int *last) { char curr_char = str[0]; int start=0;; int finish=0; int curr_start=0; int max_length = 1; for (int i=0; i max_length) { max_length = curr_length; start = curr_start; finish = i; } } else { curr_char = str[i]; curr_start = i; } } *first = start; *last = finish; return max_length; } void main(int argc, char *argv[]) { int first, last; char str1[30]="pqrpqrpqrpqrtttrrttrrrttpqrpqr"; int n = strlength(str1, 30, &first, &last); printf("String 1 = %s ",str1); printf("String length = %d, first = %d, last = %d ", n, first, last); char str2[21]="abcbbcabbabbabcbbcbca"; n = strlength(str2, 21, &first, &last); printf("String 2 = %s ",str2); printf("String length = %d, first = %d, last = %d ", n, first, last); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started