📖 Scenario: Imagine you are building a text analysis tool that finds the longest palindrome inside a given word or sentence. A palindrome is a sequence of characters that reads the same backward as forward, like "madam" or "racecar".
🎯 Goal: Build a program in C that finds and prints the longest palindromic substring from a given string.
📋 What You'll Learn
Create a character array called input with the exact value "babad"
Create an integer variable called max_len initialized to 1
Create two integer variables called start and len initialized to 0
Write a function expandAroundCenter that takes the string, left index, and right index, and returns the length of the palindrome expanding around those centers
Use a loop to check all possible centers in the string using expandAroundCenter
Update start and max_len when a longer palindrome is found
Print the longest palindromic substring using printf
💡 Why This Matters
🌍 Real World
Finding palindromic sequences is useful in text analysis, DNA sequence analysis, and data validation.
💼 Career
Understanding string manipulation and substring search is important for software developers working on text processing, search engines, and bioinformatics.
Progress0 / 4 steps
1
Create the input string
Create a character array called input and initialize it with the exact string "babad".
DSA C
Hint
Use char input[] = "babad"; to create the string.
2
Add variables to track palindrome info
Add integer variables called start and max_len. Initialize start to 0 and max_len to 1.
DSA C
Hint
Use int start = 0; and int max_len = 1;.
3
Write the expandAroundCenter function
Write a function called expandAroundCenter that takes char *s, int left, and int right. It should expand while s[left] equals s[right] and return the length of the palindrome found.
DSA C
Hint
Use a while loop to expand left and right indices while characters match.
4
Find and print the longest palindromic substring
Write a main function. Use a for loop with i from 0 to length of input - 1. For each i, call expandAroundCenter(input, i, i) and expandAroundCenter(input, i, i + 1). Update start and max_len if a longer palindrome is found. Finally, print the longest palindrome substring using printf("%.*s\n", max_len, input + start);
DSA C
Hint
Use strlen to get string length. Use printf("%.*s\n", max_len, input + start); to print the substring.
Practice
(1/5)
1. What is the main goal when finding the Longest Palindromic Substring in a given string?
easy
A. To find the longest substring with all unique characters
B. To find the longest substring that contains only vowels
C. To find the longest continuous sequence of characters that reads the same backward and forward
D. To find the longest substring that is sorted alphabetically
Solution
Step 1: Understand palindrome definition
A palindrome reads the same backward and forward, like 'madam' or 'racecar'.
Step 2: Identify substring requirement
The substring must be continuous characters in the string, not scattered.
Final Answer:
To find the longest continuous sequence of characters that reads the same backward and forward -> Option C
Hint: Look for continuous same backward-forward sequences [OK]
Common Mistakes:
Confusing substring with subsequence
Looking for unique characters instead of palindrome
Ignoring continuity of characters
2. Which of the following C code snippets correctly initializes variables to find the longest palindromic substring length?
easy
A. int maxLength = 0; int start = -1; int len = strlen(s);
B. int maxLength = 1; int start = 0; int len = strlen(s);
C. int maxLength = 1; int start = 0; int len = length(s);
D. int maxLength = 1; int start = 0; int len = s.length;
Solution
Step 1: Check correct function for string length in C
In C, strlen() is used to get string length, not length() or s.length.
Step 2: Verify variable initialization
Longest palindrome length starts at 1 (single char), start index at 0, and length is string length.
Final Answer:
int maxLength = 1; int start = 0; int len = strlen(s); -> Option B
Quick Check:
Use strlen() for string length in C [OK]
Hint: Use strlen() to get string length in C [OK]
Common Mistakes:
Using length() instead of strlen() in C
Initializing maxLength to 0 instead of 1
Using Java or C++ string length syntax in C
3. What will be the output of the following C code snippet that finds the longest palindromic substring length?
char s[] = "babad";
int maxLength = 1, start = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int low = i - 1;
int high = i + 1;
while (high < len && s[high] == s[i])
high++;
while (low >= 0 && s[low] == s[i])
low--;
while (low >= 0 && high < len && s[low] == s[high]) {
low--;
high++;
}
int length = high - low - 1;
if (maxLength < length) {
maxLength = length;
start = low + 1;
}
}
printf("%d", maxLength);
medium
A. 3
B. 2
C. 4
D. 5
Solution
Step 1: Understand the palindrome expansion logic
The code expands around each character to find palindromes, including duplicates and symmetric characters.
Step 2: Trace the longest palindrome in "babad"
Longest palindromic substrings are "bab" or "aba", both length 3.
Final Answer:
3 -> Option A
Quick Check:
Longest palindrome in "babad" = 3 [OK]
Hint: Check palindrome centers and expand outwards [OK]
Common Mistakes:
Assuming entire string is palindrome
Counting non-continuous characters
Ignoring multiple palindrome centers
4. Identify the bug in the following C code snippet intended to find the longest palindromic substring length:
char s[] = "cbbd";
int maxLength = 1, start = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int low = i - 1;
int high = i + 1;
while (low >= 0 && high < len && s[low] == s[high]) {
low--;
high++;
}
int length = high - low - 1;
if (maxLength < length) {
maxLength = length;
start = low + 1;
}
}
printf("%d", maxLength);
medium
A. The code does not check for even length palindromes correctly
B. The variable 'start' is not updated properly
C. The initial maxLength should be 0, not 1
D. The loops use wrong comparison operators
Solution
Step 1: Analyze palindrome detection logic
The code expands around a single center character, missing even length palindromes like "bb" in "cbbd".
Step 2: Identify missing even length palindrome check
Even length palindromes require checking centers between characters, which is not done here.
Final Answer:
The code does not check for even length palindromes correctly -> Option A
Quick Check:
Missing even length palindrome check = bug [OK]
Hint: Check centers between characters for even palindromes [OK]
Common Mistakes:
Only checking odd length palindromes
Not updating start index correctly
Wrong initial maxLength value
5. Given the string "forgeeksskeegfor", which approach correctly finds the longest palindromic substring and what is its length?
A) Brute force method, length 8
B) Dynamic programming method, length 12
C) Expand around center method, length 12
D) Expand around center method, length 10
hard
A. Brute force method, length 8
B. Dynamic programming method, length 12
C. Expand around center method, length 12
D. Expand around center method, length 10
Solution
Step 1: Identify longest palindrome in "forgeeksskeegfor"
The longest palindromic substring is "geeksskeeg", length 10.
Step 2: Check method correctness and length
Expand around center method can find the longest palindrome correctly. The longest palindrome is "geeksskeeg" (length 10).
Step 3: Re-examine options
Expand around center method, length 10 says expand around center method, length 10 which is correct. Other options have incorrect lengths or methods.
Final Answer:
Expand around center method, length 10 -> Option D
Quick Check:
Longest palindrome "geeksskeeg" length 10 [OK]
Hint: Longest palindrome is "geeksskeeg" length 10 [OK]
Common Mistakes:
Assuming full string is palindrome
Confusing length 12 with actual palindrome length
Mixing method with incorrect length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.