1-ANAGRAM STRING
#include <stdio.h>
#include <string.h>
// Function to check if two strings are anagrams
int areAnagrams(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);
// If lengths of strings are not equal, they cannot be anagrams
if (len1 != len2)
return 0;
// Arrays to store the count of characters in both strings
int count1[256] = {0}; // Assuming ASCII characters
int count2[256] = {0};
// Count characters in str1
for (int i = 0; i < len1; i++)
count1[str1[i]]++;
// Count characters in str2
for (int i = 0; i < len2; i++)
count2[str2[i]]++;
// Compare character counts
for (int i = 0; i < 256; i++)
if (count1[i] != count2[i])
return 0;
// If all counts match, strings are anagrams
return 1;
}
void main() {
char str1[50], str2[50];
printf("Enter the first string: ");
fgets(str1, 50, stdin);
printf("Enter the second string: ");
fgets(str2, 50, stdin);
// Check if strings are anagrams
if (areAnagrams(str1, str2))
printf("The strings are anagrams.\n");
else
printf("The strings are not anagrams.\n");
}
2-TEXT AND PATTERN
#include <stdio.h>
#include <string.h>
void findMatches(char text[], char pattern[]) {
int n = strlen(text);
int m = strlen(pattern);
int count = 0;
int i,j;
// Iterate through text string
for ( i = 0; i <n ; i++) {
// Check for pattern match starting from position i
for (j = 0; j < m; j++) {
if (text[i + j] != pattern[j])
break;
}
if (j == m) { // If pattern is found
printf("Pattern found at index %d\n", i);
count++;
}
}
printf("Total matching elements: %d\n", count);
}
int main() {
char text[] = "ababcabababc";
char pattern[] = "ab";
printf("Text: %s\n", text);
printf("Pattern: %s\n", pattern);
findMatches(text, pattern);
return 0;
}
3-RANDOMIZED QUICK SORT
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int swapCount = 0;
int comparisonCount = 0;
int shiftCount = 0;
int partition(int arr1[], int low, int high) {
// Randomly select pivot element
srand(time(NULL));
int randomIndex = low + rand() % (high - low + 1);
int pivot = arr1[randomIndex];
// Swap pivot with the first element
int temp = arr1[low];
arr1[low] = arr1[randomIndex];
arr1[randomIndex] = temp;
swapCount++;
int start = low;
int end = high;
while (start < end) {
while (arr1[start] <= pivot) {
start++;
comparisonCount++;
}
while (arr1[end] > pivot) {
end--;
comparisonCount++;
}
if (start < end) {
int temp = arr1[start];
arr1[start] = arr1[end];
arr1[end] = temp;
swapCount++;
shiftCount++;
}
}
temp = arr1[end];
arr1[end] = arr1[low];
arr1[low] = temp;
swapCount++;
shiftCount++;
return end;
}
void quicksort(int arr1[], int low, int high) {
if (low < high) {
int pivot = partition(arr1, low, high);
quicksort(arr1, low, pivot - 1);
quicksort(arr1, pivot + 1, high);
}
}
int main() {
int size1;
int i;
printf("Enter size of array: ");
scanf("%d", &size1);
int arr1[size1];
printf("Enter elements for array: ");
for (i = 0; i < size1; i++) {
scanf("%d", &arr1[i]);
}
quicksort(arr1, 0, size1 - 1);
printf("Sorted array: ");
for (int i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
}
printf("\nNumber of swaps: %d\n", swapCount);
printf("Number of comparisons: %d\n", comparisonCount);
printf("Number of shifts: %d\n", shiftCount);
return 0;
}
4- find triplet
#include <stdio.h>
void findTriplet(int arr[], int n) {
int found = 0;
// Iterate over each element in the array
for (int k = n - 1; k >= 0; k--) {
int i = 0, j = k - 1; // Set two pointers i and j
// Move pointers inward to find the pair summing to arr[k]
while (i < j) {
int sum = arr[i] + arr[j];
// If the pair sum is equal to arr[k], triplet is found
if (sum == arr[k]) {
printf("Triplet found: %d + %d = %d\n", arr[i], arr[j], arr[k]);
found = 1;
break;
} else if (sum < arr[k]) {
// If sum is less, move pointer i to the right
i++;
} else {
// If sum is greater, move pointer j to the left
j--;
}
}
if (found) break; // Exit loop if triplet is found
}
if (!found) {
printf("No such triplet found.\n");
}
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the sorted array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findTriplet(arr, n);
return 0;
}
5-Sort array by frequency
#include <stdio.h>
#define MAX_ELEMENTS 1000
// Function to count the frequency of each element in the array
void countFrequency(int arr[], int n, int frequency[]) {
for (int i = 0; i < n; i++) {
frequency[arr[i]]++;
}
}
// Custom comparison function for sorting elements by frequency in decreasing
order
int compare(int a, int b) {
// If frequencies are equal, compare values
if (a == b) return 0;
// Otherwise, compare frequencies
return (b - a);
}
// Function to perform sorting based on frequency
void sortByFrequency(int arr[], int n) {
int frequency[MAX_ELEMENTS] = {0};
// Count frequency of each element
countFrequency(arr, n, frequency);
// Sort the array using bubble sort with custom comparison
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (compare(frequency[arr[j]], frequency[arr[j + 1]]) > 0 ) {
// Swap elements if frequency of jth element is greater than (j+1)th
element
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[MAX_ELEMENTS];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sort the array by decreasing frequency
sortByFrequency(arr, n);
// Print the sorted array
printf("Sorted array by decreasing frequency:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}