0% found this document useful (0 votes)
7 views27 pages

PRACTICALS

The document contains practical programming exercises in C, including a binary search implementation for strings, an optimized bubble sort, and a selection sort for strings. Each exercise specifies constraints such as not using string functions, pointers, or parameters. Additionally, it includes modifications for input validation and case-insensitive sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views27 pages

PRACTICALS

The document contains practical programming exercises in C, including a binary search implementation for strings, an optimized bubble sort, and a selection sort for strings. Each exercise specifies constraints such as not using string functions, pointers, or parameters. Additionally, it includes modifications for input validation and case-insensitive sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PRACTICAL 5

Practical related questions.......

1] Write a C program that performs binary search on a


sorted array of strings. The program should:
• Read a sorted list of strings from the user.
• Ask the user for a target string to search for.
• Use binary search to find the target string in the list.
• Print the index of the target string if found, or a
message indicating that the string is not found.
(WITHOUT STRING FUNCTION, POINTER,
PARAMETER)

ANSWER ===>

C program that performs binary search on a sorted array


of strings without using string functions, pointers, or
parameters:

```
#include <stdio.h>

// Function to compare strings (case-insensitive)


int compareStrings(char s1[100], char s2[100]) {
int i;
for (i = 0; s1[i] && s2[i]; i++) {
char c1 = s1[i];
char c2 = s2[i];
if (c1 >= 'A' && c1 <= 'Z') c1 += 32;
if (c2 >= 'A' && c2 <= 'Z') c2 += 32;
if (c1 < c2) return -1;
if (c1 > c2) return 1;
}
if (s1[i] < s2[i]) return -1;
if (s1[i] > s2[i]) return 1;
return 0;
}

// Function for binary search


int binarySearch() {
int n;
printf("Enter the number of strings: ");
scanf("%d", &n);

char arr[10][100];
printf("Enter %d sorted strings:\n", n);
for (int i = 0; i < n; i++) {
scanf("%99s", arr[i]);
}

char target[100];
printf("Enter target string to search: ");
scanf("%99s", target);

int low = 0, high = n - 1;


while (low <= high) {
int mid = (low + high) / 2;
int cmp = compareStrings(arr[mid], target);
if (cmp == 0) return mid; // Target string found
if (cmp < 0) low = mid + 1; // Search right half
else high = mid - 1; // Search left half
}
return -1; // Target string not found
}
int main() {
int result = binarySearch();

if (result != -1) {
printf("Target string found at index %d.\n", result);
} else {
printf("Target string not found.\n");
}

return 0;
}
```

PRACTICAL 7

Practical related questions .....

1] Modify the basic Bubble Sort algorithm to include an


optimization that stops the algorithm if no swaps were
made during a pass.
WITHOUT STRING FUNCTION, POINTER,
PARAMETER

ANSWER =====>
The modified Bubble Sort algorithm with the
optimization:

```
#include <stdio.h>

// Function to swap two elements


void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

// Function for optimized Bubble Sort


int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int i, j, swapped;
for (i = 0; i < n - 1; i++) {
swapped = 0; // Initialize flag
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = 1; // Set flag if swap occurs
}
}
if (!swapped) break; // Exit loop if no swaps
}

printf("Sorted array: ");


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

---------------------------------------------------------------------------
-------
2] Modify the program to handle invalid input, such as
non-string data or strings that exceed the maximum
length.
WITHOUT STRING FUNCTION, POINTER,
PARAMETER

ANSWER =====>

The modified program:

```
#include <stdio.h>

#define MAX_LENGTH 100

// Function to read a line of input


void readLine(char arr[], int maxLen) {
int i = 0;
char c;
while ((c = getchar()) != '\n' && i < maxLen - 1) {
arr[i++] = c;
}
arr[i] = '\0';
}

// Function to check if input is valid


int isValidInput(char arr[]) {
for (int i = 0; arr[i]; i++) {
if (!(arr[i] >= 'a' && arr[i] <= 'z') &&
!(arr[i] >= 'A' && arr[i] <= 'Z') &&
!(arr[i] == ' ')) {
return 0;
}
}
return 1;
}

// Function to swap two elements


void swap(char arr1[], char arr2[]) {
char temp[MAX_LENGTH];
strcpy(temp, arr1);
// Simulate strcpy without string function
for (int i = 0; temp[i]; i++) {
arr1[i] = arr2[i];
}
for (int i = 0; temp[i]; i++) {
arr2[i] = temp[i];
}
}

// Function for optimized Bubble Sort


int main() {
int n;
printf("Enter the number of elements (max 10): ");
scanf("%d");
getchar(); // Consume newline

// Input validation for n


if (n < 1 || n > 10) {
printf("Invalid input. Please enter a number
between 1 and 10.");
return 1;
}

char arr[10][MAX_LENGTH];
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
printf("String %d: ", i + 1);
readLine(arr[i], MAX_LENGTH);

// Input validation for string length


if (arr[i][MAX_LENGTH - 1] != '\0') {
printf("String too long. Please enter a string with
max %d characters.", MAX_LENGTH - 1);
return 1;
}

// Input validation for non-string data


if (!isValidInput(arr[i])) {
printf("Invalid input. Please enter only alphabets
and spaces.");
return 1;
}
}

int i, j, swapped;
for (i = 0; i < n - 1; i++) {
swapped = 0;
for (j = 0; j < n - i - 1; j++) {
// Compare strings without string function
int k;
for (k = 0; arr[j][k] && arr[j + 1][k]; k++) {
if (arr[j][k] > arr[j + 1][k]) break;
if (arr[j][k] < arr[j + 1][k]) break;
}
if (arr[j][k] > arr[j + 1][k] || (arr[j][k] == '\0' && arr[j
+ 1][k] != '\0')) {
swap(arr[j], arr[j + 1]);
swapped = 1;
}
}
if (!swapped) break;
}

printf("Sorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}

return 0;
}
```
PRACTICAL 9

PRACTICAL RELATED QUESTIONS.......

1] Write a C program to sort an array of Strings using


Selection Sort.
WITHOUT STRING FUNCTION, POINTER,
PARAMETER

ANSWER =====>

C program to sort an array of strings using Selection


Sort:

```
#include <stdio.h>

#define MAX_LENGTH 100


#define MAX_ELEMENTS 10

// Function to swap two strings


void swap(char arr1[], char arr2[]) {
char temp[MAX_LENGTH];
int i;
for (i = 0; arr1[i]; i++) {
temp[i] = arr1[i];
}
temp[i] = '\0';
for (i = 0; arr2[i]; i++) {
arr1[i] = arr2[i];
}
arr1[i] = '\0';
for (i = 0; temp[i]; i++) {
arr2[i] = temp[i];
}
arr2[i] = '\0';
}

// Function to compare two strings


int compareStrings(char str1[], char str2[]) {
int i;
for (i = 0; str1[i] && str2[i]; i++) {
if (str1[i] < str2[i]) return -1;
if (str1[i] > str2[i]) return 1;
}
if (str1[i] == '\0' && str2[i] != '\0') return -1;
if (str1[i] != '\0' && str2[i] == '\0') return 1;
return 0;
}

// Function for Selection Sort


int main() {
int n;
printf("Enter the number of strings (max %d): ",
MAX_ELEMENTS);
scanf("%d");
getchar(); // Consume newline

// Input validation for n


if (n < 1 || n > MAX_ELEMENTS) {
printf("Invalid input. Please enter a number
between 1 and %d.", MAX_ELEMENTS);
return 1;
}

char arr[MAX_ELEMENTS][MAX_LENGTH];
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
printf("String %d: ", i + 1);
int j = 0;
char c;
while ((c = getchar()) != '\n' && j < MAX_LENGTH -
1) {
arr[i][j++] = c;
}
arr[i][j] = '\0';
}

int i, j, minIndex;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (compareStrings(arr[j], arr[minIndex]) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}

printf("Sorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}

return 0;
}
```

---------------------------------------------------------------------------
-------
2] Extend your Selection Sort implementation to support
case-insensitive sorting of strings. Ensure that
uppercase and lowercase versions of the same letter
are treated as equal during sorting. WITHOUT STRING
FUNCTION, POINTER, PARAMETER
ANSWER =====>

The extended Selection Sort implementation to support


case-insensitive sorting of strings:

```
#include <stdio.h>

#define MAX_LENGTH 100


#define MAX_ELEMENTS 10

// Function to swap two strings


void swap(char arr1[], char arr2[]) {
char temp[MAX_LENGTH];
int i;
for (i = 0; arr1[i]; i++) {
temp[i] = arr1[i];
}
temp[i] = '\0';
for (i = 0; arr2[i]; i++) {
arr1[i] = arr2[i];
}
arr1[i] = '\0';
for (i = 0; temp[i]; i++) {
arr2[i] = temp[i];
}
arr2[i] = '\0';
}

// Function to compare two strings (case-insensitive)


int compareStrings(char str1[], char str2[]) {
int i;
for (i = 0; str1[i] && str2[i]; i++) {
char c1 = str1[i];
char c2 = str2[i];
if (c1 >= 'A' && c1 <= 'Z') c1 += 32;
if (c2 >= 'A' && c2 <= 'Z') c2 += 32;
if (c1 < c2) return -1;
if (c1 > c2) return 1;
}
if (str1[i] == '\0' && str2[i] != '\0') return -1;
if (str1[i] != '\0' && str2[i] == '\0') return 1;
return 0;
}

// Function for Selection Sort


int main() {
int n;
printf("Enter the number of strings (max %d): ",
MAX_ELEMENTS);
scanf("%d");
getchar(); // Consume newline

// Input validation for n


if (n < 1 || n > MAX_ELEMENTS) {
printf("Invalid input. Please enter a number
between 1 and %d.", MAX_ELEMENTS);
return 1;
}

char arr[MAX_ELEMENTS][MAX_LENGTH];
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
printf("String %d: ", i + 1);
int j = 0;
char c;
while ((c = getchar()) != '\n' && j < MAX_LENGTH -
1) {
arr[i][j++] = c;
}
arr[i][j] = '\0';
}

int i, j, minIndex;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (compareStrings(arr[j], arr[minIndex]) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}

printf("Sorted strings (case-insensitive):\n");


for (i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}

return 0;
}
```

---------------------------------------------------------------------------
-------
😂✌️

You might also like