Functions and Recursions – Strings – Problem #4

IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive

Difficulty: hard

Problem Statement

A string S is said to have a palindrome substring of length k (k must be > 1) if there is a substring P of length k in S which is a palindrome. For example, the string “abababc” has two palindrome substrings of length 5 (“babab” and “ababa”). It also has several palindrome substrings of length 3 (eg., “aba”, etc). You are to read a string S and find the largest k (k>1) such that there is a palindrome substring P of length k in S, and print P and k. (If there are multiple such palindrome substrings of length k, print all of them). For this, you will write a non-recursive functiopal(S[],i,j) which takes as arguments address of a string array s and integer indices i and j and returns 1 if the substring formed by the i_th to j_th consecutive characters of S form a palindrome. Otherwise it returns 0. For example if S “abababc”, the call pal(S,2,6) will return 1 (since “babab” is a palindrome) while pal(S,1,4) will return 0 (since “abab” is not a palindrome). This function will be repeatedly called in the main program to solve the problem. [Do not distinguish between upper and lower case] Save the file as A10_<Roll Number>.c (example A10_21AG10002.c). Build, Run and Test it for the given data as well as your own data. Then upload the .c file for the Assignment. [20 Marks: 5 marks for intermediate submission, 15 marks for final submission] In particular, you will do the following: 1. Read in a string S and print it. Create a new line. 2. Define and call the function pal appropriately to find all palindrome substrings of length k > 1 in S. Print each of them along with their i, j indices. 3. Find the largest palindrome substring of k > 1 in s and print all of them. If no such, print “No such”. 4. [Bonus] If you complete these within the scheduled time, write the function pal in a recursive manner. Call it rpal and use appropriate parameters. Test it like you test pal. [Bonus will not add to assignment marks] [Do not call any Library Functions other than scanf, printf. You may use your own user defined functions] Test Data: ababababc, Malayalam, Bengal, Tatlallay.

Back to Lab Problems
Functions and Recursions/Strings/Problem #4

Lab Problem Statement

Specifications & Problem DetailsIIT Kharagpur PDS Archive
Loading question...

Interactive C Compiler · Problem #4

WASM In-Browser

Runs directly on your machine · Zero remote server dependency

Compiler downloads on first Run (large, saved in your browser after that)
main.cCtrl + Enter to run
1
2
3
4
5
6
7
Input (stdin)given to scanf / cin
Output

Output appears here. Press Run to compile and execute.