#include <stdio.
h>
#include <string.h>
#define MAX_SIZE 15
void sortArray(char arr[], int n) {
char temp;
for (int i = 0; i < n-1; ++i) {
for (int j = i+1; j < n; ++j) {
if (arr[i] > arr[j]) {
// Swap
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
char arr[MAX_SIZE + 1]; // +1 for null terminator
char ch;
char ch;
int count = 0;
// Reading characters into the array
printf("Enter up to 15 characters (no spaces): ");
scanf("%15s", arr); // Read string of characters,
automatically null-terminated
int len = strlen(arr);
// Print original array
printf("Original array: %s\n", arr);
// Sort the array
sortArray(arr, len);
// Print sorted array
printf("Sorted array: %s\n", arr);
// Read a character to find its frequency
printf("Enter a character to search: ");
scanf(" %c", &ch); // Note the space before %c to
consume any leftover newline
// Count occurrences
for (int i = 0; i < len; ++i) {
if (arr[i] == ch) {
++count;
}
}
printf("Character '%c' occurs %d time(s) in the
array.\n", ch, count);
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX_PRODUCTS 5
// Structure for date
struct date {
int day;
int month;
};
// Structure for product
struct product {
struct product {
char pname[21]; // 20 characters + null terminator
unsigned qty;
struct date mfg;
};
// Function to read product details
void readProducts(struct product products[], int n) {
for (int i = 0; i < n; ++i) {
printf("Enter details for product %d:\n", i + 1);
printf("Product name: ");
getchar(); // consume leftover newline
fgets(products[i].pname, 21, stdin);
// remove trailing newline
products[i].pname[strcspn(products[i].pname, "\n")] = '\0';
printf("Quantity (kg): ");
scanf("%u", &products[i].qty);
printf("Manufacturing date (dd mm): ");
scanf("%d %d", &products[i].mfg.day, &products[i].mfg.month);
}
}
// Helper function to compare two dates
int isBetween(struct date d, struct date start, struct date end) {
if ((d.month > start.month || (d.month == start.month && d.day >= start.day)) &&
(d.month < end.month || (d.month == end.month && d.day <= end.day))) {
return 1;
}
return 0;
}
// Function to print products within date range
void printProductsInRange(struct product products[], int n, struct date start, struct date end) {
printf("\nFruits manufactured between %02d/%02d and %02d/%02d:\n",
start.day, start.month, end.day, end.month);
for (int i = 0; i < n; ++i) {
if (isBetween(products[i].mfg, start, end)) {
printf("Name: %s, Qty: %u kg, Date: %02d/%02d\n",
printf("Name: %s, Qty: %u kg, Date: %02d/%02d\n",
products[i].pname, products[i].qty,
products[i].mfg.day, products[i].mfg.month);
}
}
}
// Main program
int main() {
struct product fruits[MAX_PRODUCTS];
struct date startDate, endDate;
// Read 5 fruits
readProducts(fruits, MAX_PRODUCTS);
// Read date range
printf("\nEnter start date (dd mm): ");
scanf("%d %d", &startDate.day, &startDate.month);
printf("Enter end date (dd mm): ");
scanf("%d %d", &endDate.day, &endDate.month);
// Display products in range
printProductsInRange(fruits, MAX_PRODUCTS, startDate, endDate);
return 0;
}
3a
#include <stdio.h>
#define MAX 51
void concatUsingPointers(char *str1, char *str2, int n) {
// Move str1 pointer to the end
while (×str1 != '\0') {
str1++;
}
// Append first n characters of str2
for (int i = 0; i < n && str2[i] != '\0'; i++) {
×str1 = str2[i];
str1++;
}
×str1 = '\0'; // Null-terminate the result
}
int main() {
char str1[MAX], str2[MAX];
int n;
printf("Enter first string: ");
fgets(str1, MAX, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string: ");
fgets(str2, MAX, stdin);
str2[strcspn(str2, "\n")] = '\0';
printf("Enter positive integer n: ");
scanf("%d", &n);
concatUsingPointers(str1, str2, n);
printf("Concatenated string (pointers): %s\n", str1);
return 0;
}
b.
#include <stdio.h>
#include <string.h>
#define MAX 51
int main() {
char str1[MAX * 2], str2[MAX];
int n;
printf("Enter first string: ");
getchar(); // Clear input buffer if needed
fgets(str1, MAX, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string: ");
fgets(str2, MAX, stdin);
str2[strcspn(str2, "\n")] = '\0';
printf("Enter positive integer n: ");
scanf("%d", &n);
// Use temporary string to copy first n characters
char temp[MAX];
strncpy(temp, str2, n);
temp[n] = '\0'; // Make sure it's null-terminated
strcat(str1, temp);
printf("Concatenated string (built-in): %s\n", str1);
return 0;
}
4.
#include <stdio.h>
// Function to print a 16-bit binary representation
void printBinary(unsigned short num) {
for (int i = 15; i >= 0; i--) {
printf("%d", (num >> i) & 1);
if (i % 4 == 0) printf(" ");
}
printf("\n");
}
// -----------------------------------------
// PART a: Check if any of bits 2, 5, 7, or 15 is set to 1
void checkAnyBits(unsigned short num) {
unsigned short MASK = (1 << 2) | (1 << 5) | (1 << 7) | (1 << 15);
printf("Value: %u\n", num);
printf("Binary: ");
printBinary(num);
if (num & MASK) {
printf("→ At least one of bits 2, 5, 7 or 15 is set to 1.\n");
} else {
printf("→ None of bits 2, 5, 7 or 15 is set to 1.\n");
}
printf("\n");
}
// -----------------------------------------
// PART b: Check if all of bits 3, 7, and 13 are 0
void checkAllBitsFalse(unsigned short num) {
unsigned short MASK = (1 << 3) | (1 << 7) | (1 << 13);
printf("Value: %u\n", num);
printf("Binary: ");
printBinary(num);
if ((num & MASK) == 0) {
printf("→ All of bits 3, 7, and 13 are 0.\n");
} else {
printf("→ At least one of bits 3, 7, or 13 is set to 1.\n");
}
printf("\n");
}
// -----------------------------------------
// PART c: Set (force to 1) bits 1, 8, 12, and 14
void setBits(unsigned short num) {
unsigned short MASK = (1 << 1) | (1 << 8) | (1 << 12) | (1 << 14);
printf("Original value: %u\n", num);
printf("Original binary: ");
printBinary(num);
num = num | MASK;
printf("→ After setting bits: ");
printBinary(num);
printf("\n");
}
// -----------------------------------------
// PART d: Toggle bits 2, 7, 11, and 12
void toggleBits(unsigned short num) {
unsigned short MASK = (1 << 2) | (1 << 7) | (1 << 11) | (1 << 12);
printf("Original value: %u\n", num);
printf("Original binary: ");
printBinary(num);
num = num ^ MASK;
printf("→ After toggling bits: ");
printBinary(num);
printf("\n");
}
// -----------------------------------------
// MAIN FUNCTION
int main() {
unsigned short value;
// Read and display the binary of any input value
printf("Enter a decimal number (0–65535): ");
scanf("%hu", &value);
printf("2-byte binary representation:\n");
printBinary(value);
printf("\n");
// Run all parts of the task
// a. Check if any of bits 2, 5, 7, or 15 are 1
printf("----- PART a -----\n");
checkAnyBits(23094);
checkAnyBits(26963);
// b. Check if bits 3, 7, and 13 are all 0
printf("----- PART b -----\n");
checkAllBitsFalse(39478);
checkAllBitsFalse(22933);
// c. Set bits 1, 8, 12, and 14
printf("----- PART c -----\n");
setBits(39606);
// d. Toggle bits 2, 7, 11, and 12
printf("----- PART d -----\n");
toggleBits(21308);
return 0;
}