You are on page 1of 7

1. Write a program to print all permutations of a string.

#include <stdio.h>
#include <conio.h>
#include <string.h>

/* Function to swap values at two pointers */


void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}

/* Function to print permutations of string


This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int l, int r)
{
int i;
if (l == r)
printf("%s\n", a);
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i)); //backtrack
}
}
}

/* Driver program to test above functions */


int main()
{

char str[10];
int n;
strcpy(str,"ABC");
n = strlen(str);
permute(str, 0, n-1);
getch();
return 0;
}
2. Write a program to find the longest palindrome in a string.

#include <stdio.h>
#include <conio.h>
#include <string.h>
// A utility function to print a substring str[low..high]
void printSubStr(char* str, int low, int high)
{ int i;
for(i=low; i<=high; ++i)
printf("%c", str[i]);
}
// This function prints the longest palindrome substring (LPS)
// of str[]. It also returns the length of the longest palindrome
int longestPalSubstr(char *str)
{
int maxLength = 1; // The result (length of LPS)

int start = 0;
int len=strlen(str);

int low, high,i;

// One by one consider every character as center point of


// even and length palindromes
for (i=1; i<len; ++i)
{
// Find the longest even length palindrome with center points
// as i-1 and i.
low = i - 1;
high = i;
while (low >= 0 && high < len && str[low] == str[high])
{
if (high - low + 1 > maxLength)
{
start = low;
maxLength = high - low + 1;
}
--low;
++high;
}

// Find the longest odd length palindrome with center


// point as i
low = i - 1;
high = i + 1;
while (low >= 0 && high < len && str[low] == str[high])
{
if (high - low + 1 > maxLength)
{
start = low;
maxLength = high - low + 1;
}
--low;
++high;
}
}

printf("Longest palindrome substring is: ");


printSubStr(str, start, start + maxLength - 1);

return maxLength;
}

int main()
{
char str[30];
clrscr();
str="Helloabcdedcbahello";
printf("\nLength is: %d", longestPalSubstr(str) );
return 0;
}
3. Write a program to take two array and find which number is
not present in the second array and it is prime.
#include<iostream.h>
#include<conio.h>
// Function for finding
// elements which are there
// in a[] but not in b[].
void findMissing(int a[], int b[],
int n, int m)
{
for (int i = 0; i < n; i++)
{
int j,flag=0;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break;

if (j == m)
{

for(j = 2; j <= a[i]/2; j++)


{
if(a[i] % j == 0)
{
flag=1;
break;
}
}
if (flag==0)
cout << a[i] << " ";
}
}
}

// Driver code
int main()
{
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[1]);
findMissing(a, b, n, m);
getch();
return 0;
}
4. Rearrange the array with alternative high and low elements.
#include <stdio.h>
#include <conio.h>

// Utility function to swap two elements arr[i] and arr[j] in


the array
void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

// Function to rearrange the array such that every second


element
// of the array is greater than its left and right elements
void rearrangeArray(int arr[], int n)
{
// start from second element and increment index
// by 2 for each iteration of loop
int i;
for (i = 1; i < n; i += 2)
{
// If the prev element is greater than current
element,
// swap the elements
if (arr[i - 1] > arr[i]) {
swap(arr, i-1, i);
}

// if next element is greater than current element,


// swap the elements
if (i + 1 < n && arr[i + 1] > arr[i]) {
swap(arr, i+1, i);
}}}
// main function
int main(void)
{
int i, arr[] = { 9, 6, 8, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);

rearrangeArray(arr, n);

// print output array


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
getch();
return 0;
}

You might also like