debug this code #include<stdio.h> #include<stdlib.h> #include<string.h> int…
- User
debug this code #include<stdio.h> #include<stdlib.h> #include<string.h>
int main_func(char str[], int start, int end) { int len = 0; while(start >= 0 && end < strlen(str) && str[start] == str[end]) { len += 2; start--; end++; } return len; }
int main() { char str[1000]; scanf("%s", str);
int max = 0; for(int j = 0; j < strlen(str); j++) { int len1 = main_func(str, j, j); int len2 = main_func(str, j, j); max = (max < len1) ? len1 : max; max = (max < len2) ? len2 : max; } printf("%d", max); return 0;}
- Assistant
The issue is in the for loop where function main_func is called twice but with the same parameters. The second parameter of the second call should be j+1 instead of j. Here is the corrected code:
#include<stdio.h> #include<stdlib.h> #include<string.h>
int main_func(char str[], int start, int end) { int len = 0; while(start >= 0 && end < strlen(str) && str[start] == str[end]) { len += 2; start--; end++; } return len; }
int main() { char str[1000]; scanf("%s", str);
int max = 0; for(int j = 0; j < strlen(str); j++) { int len1 = main_func(str, j, j); int len2 = main_func(str, j, j+1); // fixed parameter here max = (max < len1) ? len1 : max; max = (max < len2) ? len2 : max; } printf("%d", max); return 0;}