Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print the arranged positions of characters to make palindrome in C Program.
You are provided with a string str with some length n. Print the position of every element of the string so it can form a palindrome, else print a message "No palindrome" on screen.
What is palindrome?
Palindrome is a word, sequence of characters which reads same from the reverse or backward as from the forward manner, like MADAM, racecar.
To find a sequence or a word is palindrome we generally store the reverse of a word in a separate string and compare both if they are same then the given word or sequence is palindrome. But in this question we have to print the arrangement to make a word or a sequence in palindrome.
Like, there is a string str = "tinni" then it can be intni or nitin so we have to return any one of the sequence of arrangement as index starting from 1 and result can be either 2 3 1 4 5 or 3 2 1 5 4 of the two.
Syntax
void printPalindromePos(char str[], int n);
Algorithm
The algorithm follows these key steps −
- Store positions of each character in the string
- Count characters with odd frequency (at most 1 allowed for palindrome)
- Print first half positions, then odd character (if any), then second half in reverse
Example
Here's a complete C program to print palindrome arrangement positions −
#include <stdio.h>
#include <string.h>
#define MAX 256
void printPalindromePos(char str[]) {
int pos[MAX][100]; // Store positions for each character
int count[MAX] = {0}; // Count of each character
int n = strlen(str);
// Store positions of each character
for (int i = 0; i < n; i++) {
pos[str[i]][count[str[i]]] = i + 1;
count[str[i]]++;
}
// Count characters with odd frequency
int oddCount = 0;
char oddChar = 0;
for (int i = 0; i < MAX; i++) {
if (count[i] % 2 != 0) {
oddCount++;
oddChar = i;
}
}
// Palindrome can't have more than 1 odd character
if (oddCount > 1) {
printf("NO PALINDROME<br>");
return;
}
// Print first half positions
for (int i = 0; i < MAX; i++) {
int mid = count[i] / 2;
for (int j = 0; j < mid; j++) {
printf("%d ", pos[i][j]);
}
}
// Print odd character position (middle)
if (oddCount > 0) {
printf("%d ", pos[oddChar][count[oddChar] - 1]);
}
// Print second half positions in reverse
for (int i = MAX - 1; i >= 0; i--) {
int total = count[i];
for (int j = total / 2; j < total - (oddCount > 0 && i == oddChar ? 1 : 0); j++) {
printf("%d ", pos[i][j]);
}
}
printf("<br>");
}
int main() {
char str1[] = "tinni";
char str2[] = "baa";
char str3[] = "abc";
printf("Input: %s\nOutput: ", str1);
printPalindromePos(str1);
printf("Input: %s\nOutput: ", str2);
printPalindromePos(str2);
printf("Input: %s\nOutput: ", str3);
printPalindromePos(str3);
return 0;
}
Input: tinni Output: 2 3 1 4 5 Input: baa Output: 2 1 3 Input: abc Output: NO PALINDROME
How It Works
The algorithm works by storing character positions and analyzing frequency patterns. For a valid palindrome arrangement −
- At most one character can have odd frequency (goes in middle)
- All other characters must have even frequency (split equally on both sides)
- Positions are printed as: first half + middle (if any) + second half reversed
Conclusion
This approach efficiently determines if a string can form a palindrome and prints the required character positions. The algorithm has O(n) time complexity where n is the string length.
