
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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.
The above problem needs a solution like the given example below −
Example
Input: string str = “baa” Output: 2 1 3 Input: string str = “tinni” Output: 2 3 1 4 5
Algorithm
void printPalindromePos(string &str) START STEP 1: DECLARE vector<int> pos[MAX] STEP 2: DECLARE AND ASSIGN n WITH LENGTH OF str STEP 3: LOOP FOR i = 0 AND i < n AND i++ pos[str[i]].push_back(i+1) END LOOP STEP 4: SET oddCount = 0 STEP 5: DECLARE oddChar STEP 6: LOOP FOR i=0 AND i<MAX AND i++ IF pos[i].size() % 2 != 0 THEN, INCREMENT oddCount BY 1 SET oddChar AS i END IF END FOR STEP 7: IF oddCount > 1 THEN, PRINT "NO PALINDROME" STEP 8: LOOP FOR i=0 AND i<MAX AND i++ DECRLARE mid = pos[i].size()/2 LOOP FOR j=0 AND j<mid AND j++ PRINT pos[i][j] END LOOP END LOOP STEP 9: IF oddCount > 0 THEN, DECLARE AND SET last = pos[oddChar].size() - 1 PRINT pos[oddChar][last] SET pos[oddChar].pop_back(); END IF STEP 10: LOOP FOR i=MAX-1 AND i>=0 AND i-- DECLARE AND SET count = pos[i].size() LOOP FOR j=count/2 AND j<count AND j++ PRINT pos[i][j] STOP
Example
#include <bits/stdc++.h> using namespace std; // Giving the maximum characters const int MAX = 256; void printPalindromePos(string &str){ //Inserting all positions of characters in the given string. vector<int> pos[MAX]; int n = str.length(); for (int i = 0; i < n; i++) pos[str[i]].push_back(i+1); /* find the number of odd elements.Takes O(n) */ int oddCount = 0; char oddChar; for (int i=0; i<MAX; i++) { if (pos[i].size() % 2 != 0) { oddCount++; oddChar = i; } } /* Palindrome can't contain more than 1 odd characters */ if (oddCount > 1) cout << "NO PALINDROME"; /* Print positions in first half of palindrome */ for (int i=0; i<MAX; i++){ int mid = pos[i].size()/2; for (int j=0; j<mid; j++) cout << pos[i][j] << " "; } // Consider one instance odd character if (oddCount > 0){ int last = pos[oddChar].size() - 1; cout << pos[oddChar][last] << " "; pos[oddChar].pop_back(); } /* Print positions in second half of palindrome */ for (int i=MAX-1; i>=0; i--){ int count = pos[i].size(); for (int j=count/2; j<count; j++) cout << pos[i][j] << " "; } } int main(){ string s = "tinni"; printPalindromePos(s); return 0; }
Output
If we run above program then it will generate following output −
2 3 1 4 5
- Related Articles
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to find minimum number of characters to be added to make it palindrome in Python
- Print longest palindrome word in a sentence in C Program
- C program to print characters and strings in different formats.
- C program to print characters without using format specifiers
- Rearrange characters to form palindrome if possible in C++
- Minimum removal to make palindrome permutation in C++
- Write a program in C++ to split two strings to make it a palindrome
- Program to get final string after shifting characters with given number of positions in Python
- Python program to print palindrome triangle with n lines
- Print all palindrome permutations of a string in C++
- Java program to print whether the given string is a palindrome
- Ways to print escape characters in C#
- Minimum number of deletions to make a string palindrome in C++.
- Program to split two strings to make palindrome using Python
