0% found this document useful (0 votes)
21 views21 pages

1

Uploaded by

Alieu Keita
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)
21 views21 pages

1

Uploaded by

Alieu Keita
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/ 21

1.

#include <stdio.h>

void main() {

int arr1[] = {1, 3, 4, 5, 7};

int arr2[] = {2, 3, 5, 6};

int n = sizeof(arr1) / sizeof(arr1[0]);

int m = sizeof(arr2) / sizeof(arr2[0]);

int i = 0, j = 0;

printf("Union: ");

while (i < n && j < m) {

if (arr1[i] < arr2[j]) {

printf("%d ", arr1[i++]);

} else if (arr2[j] < arr1[i]) {

printf("%d ", arr2[j++]);

} else {

printf("%d ", arr2[j++]);

i++;

while (i < n) {

printf("%d ", arr1[i++]);

while (j < m) {

printf("%d ", arr2[j++]);

printf("\n");

i = 0, j = 0;

printf("Intersection: ");

while (i < n && j < m) {

if (arr1[i] < arr2[j]) {

i++;

} else if (arr2[j] < arr1[i]) {

j++;
} else {

printf("%d ", arr2[j++]);

i++;

printf("\n");

}
2.

#include <stdio.h>

int main() {
int i, j, rows, columns, a[10][10], Flag = 1;

printf("\nPlease Enter Number of rows and columns : ");

scanf("%d %d", &i, &j);

printf("\nPlease Enter the Matrix Elements\n");

for (rows = 0; rows < i; rows++) {

for (columns = 0; columns < j; columns++) {

scanf("%d", &a[rows][columns]);

for (rows = 0; rows < i; rows++) {

for (columns = 0; columns < j; columns++) {

if (a[rows][columns] != 1 && a[columns][rows] != 0) {

Flag = 0;

break;

if (Flag == 1) {

printf("\nThe Matrix that you entered is an Identity Matrix\n");

} else {

printf("\nThe Matrix that you entered is Not an Identity Matrix\n");

return 0;

}
3.

#include <stdio.h>

#define ROW 10

#define COL 10

void sort_rows(int arr[][COL], int n) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < COL; j++) {

for (int k = j + 1; k < COL; k++) {

if (arr[i][j] > arr[i][k]) {

int temp = arr[i][j];

arr[i][j] = arr[i][k];

arr[i][k] = temp;

int main() {

int n;

printf("Enter the order of the matrix: ");

scanf("%d", &n);

int arr[ROW][COL];

printf("Enter the matrix elements:\n");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

scanf("%d", &arr[i][j]);

sort_rows(arr, n);

printf("Matrix after sorting row elements in ascending order:\n");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {


printf("%d ", arr[i][j]);

printf("\n");

return 0;

}
4.
5.
6.
#include <stdio.h>
#include <stdlib.h>
struct node {

int data;

struct node *left;

struct node *right;

};

struct node *new_node(int data) {

struct node *node = (struct node *)malloc(sizeof(struct node));

node->data = data;

node->left = NULL;

node->right = NULL;

return node;

struct node *insert(struct node *node, int data) {

if (node == NULL) {

return new_node(data);

} else {

if (data <= node->data) {

node->left = insert(node->left, data);

} else {

node->right = insert(node->right, data);

return node;

struct node *search(struct node *node, int data) {

if (node == NULL || node->data == data) {

return node;

} else if (data < node->data) {

return search(node->left, data);

} else {

return search(node->right, data);

}
}

int main() {

struct node *root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

int x;

printf("Enter the element to search: ");

scanf("%d", &x);

struct node *result = search(root, x);

if (result == NULL) {

printf("Element not found\n");

} else {

printf("Element found\n");

return 0;

}
7.
#include <stdio.h>
#include <string.h>
int length_of_longest_substring(char *str) {

int n = strlen(str);

int longest_str_len = 1;

int current_substr_len = 1;

int previous_index, i;

int temp[256];

memset(temp, -1, sizeof(int) * 256);

temp[str[0]] = 0;

for (i = 1; i < n; i++) {

previous_index = temp[str[i]];

if (previous_index == -1 || i - current_substr_len > previous_index) {

current_substr_len++;

} else {

if (current_substr_len > longest_str_len) {

longest_str_len = current_substr_len;

}
current_substr_len = i - previous_index;
}
temp[str[i]] = i;
}
if (current_substr_len > longest_str_len) {

longest_str_len = current_substr_len;

}
return longest_str_len;
}

int main() {

char str[] = "xyzwwepppw";

printf("Input string: \"%s\"\n", str);

printf("Longest substring without repeating characters: %d\n", length_of_longest_substring(str));

return 0;
}
8.
#include <stdio.h>
#include <stdlib.h>

struct Employee {

char name[50];

char department[50];

int eid;

float salary;

};

int main() {

int n;

printf("Enter the number of employees: ");

scanf("%d", &n);

struct Employee *employees = (struct Employee *)malloc(n * sizeof(struct Employee));

for (int i = 0; i < n; i++) {

printf("Enter details of employee %d:\n", i + 1);

printf("Name: ");

scanf("%s", employees[i].name);

printf("Department: ");

scanf("%s", employees[i].department);

printf("Employee ID: ");

scanf("%d", &employees[i].eid);

printf("Salary: ");

scanf("%f", &employees[i].salary);

printf("\nEmployee details:\n");

for (int i = 0; i < n; i++) {

printf("Name: %s\n", employees[i].name);

printf("Department: %s\n", employees[i].department);

printf("Employee ID: %d\n", employees[i].eid);

printf("Salary: %.2f\n", employees[i].salary);

printf("\n");

}
free(employees);

return 0;

}
9.

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100

struct Student {

char name[50];

int rollno;

float marks;

};

void insert(struct Student *students, int *n) {

printf("Enter the details of the student:\n");

printf("Name: ");

scanf("%s", students[*n].name);

printf("Roll number: ");

scanf("%d", &students[*n].rollno);

printf("Marks: ");

scanf("%f", &students[*n].marks);

(*n)++;

void display(struct Student *students, int n) {

printf("Student details:\n");

for (int i = 0; i < n; i++) {

printf("Name: %s\n", students[i].name);

printf("Roll number: %d\n", students[i].rollno);

printf("Marks: %.2f\n", students[i].marks);

printf("\n");

void delete(struct Student *students, int *n) {

int rollno;

printf("Enter the roll number of the student to delete: ");

scanf("%d", &rollno);

int i;

for (i = 0; i < *n; i++) {


if (students[i].rollno == rollno) {

break;

if (i == *n) {

printf("Student with roll number %d not found\n", rollno);

} else {

for (int j = i; j < *n - 1; j++) {

students[j] = students[j + 1];

(*n)--;

printf("Student with roll number %d deleted successfully\n", rollno);

void search(struct Student *students, int n) {

int rollno;

printf("Enter the roll number of the student to search: ");

scanf("%d", &rollno);

int i;

for (i = 0; i < n; i++) {

if (students[i].rollno == rollno) {

printf("Student details:\n");

printf("Name: %s\n", students[i].name);

printf("Roll number: %d\n", students[i].rollno);

printf("Marks: %.2f\n", students[i].marks);

printf("\n");

break;

if (i == n) {

printf("Student with roll number %d not found\n", rollno);

}
int main() {

struct Student students[MAX_SIZE];

int n = 0;

int choice;

do {

printf("Menu:\n");

printf("1. Insert\n");

printf("2. Display\n");

printf("3. Delete\n");

printf("4. Search\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

insert(students, &n);

break;

case 2:

display(students, n);

break;

case 3:

delete(students, &n);

break;

case 4:

search(students, n);

break;

case 5:

printf("Exiting...\n");

break;

default:

printf("Invalid choice\n");

} while (choice != 5);


return 0;

}
11.

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

int main() {

FILE *fptr;

char filename[] = "c:\\myfile.txt";

char ch;

int alphabets = 0, digits = 0, whitespace = 0, special_chars = 0, lines = 0;

if ((fptr = fopen(filename, "w")) == NULL) {

printf("Error opening file\n");

exit(EXIT_FAILURE);

printf("Enter some text: ");

while ((ch = getchar()) != EOF) {

fputc(ch, fptr);

fclose(fptr);

if ((fptr = fopen(filename, "r")) == NULL) {

printf("Error opening file\n");

exit(EXIT_FAILURE);

while ((ch = fgetc(fptr)) != EOF) {

if (isalpha(ch)) {

alphabets++;

} else if (isdigit(ch)) {

digits++;

} else if (isspace(ch)) {

whitespace++;

} else {

special_chars++;

if (ch == '\n') {
lines++;

printf("File contents:\n");

rewind(fptr);

while ((ch = fgetc(fptr)) != EOF) {

putchar(ch);

printf("\n");

printf("Total alphabets: %d\n", alphabets);

printf("Total digits: %d\n", digits);

printf("Total whitespace characters: %d\n", whitespace);

printf("Total special characters: %d\n", special_chars);

printf("Total lines: %d\n", lines);

fclose(fptr);

return 0;

}
12.

#include <stdio.h>

#include <stdlib.h>

struct Car {

char model[50];

int year;

float engine_cap;

char colour[50];

float price;

};

int main() {

struct Car car;

FILE *fptr;

char filename[] = "d:\\mycar\\car.dat";

if ((fptr = fopen(filename, "wb")) == NULL) {

printf("Error opening file\n");

exit(EXIT_FAILURE);

printf("Enter car details:\n");

printf("Model: ");

scanf("%s", car.model);

printf("Year: ");

scanf("%d", &car.year);

printf("Engine capacity: ");

scanf("%f", &car.engine_cap);

printf("Colour: ");

scanf("%s", car.colour);

printf("Price: ");

scanf("%f", &car.price);

fwrite(&car, sizeof(struct Car), 1, fptr);

fclose(fptr);
if ((fptr = fopen(filename, "rb")) == NULL) {

printf("Error opening file\n");

exit(EXIT_FAILURE);

fread(&car, sizeof(struct Car), 1, fptr);

printf("Car details:\n");

printf("Model: %s\n", car.model);

printf("Year: %d\n", car.year);

printf("Engine capacity: %.2f\n", car.engine_cap);

printf("Colour: %s\n", car.colour);

printf("Price: %.2f\n", car.price);

fclose(fptr);

return 0;

}
13.

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

int main(int argc, char *argv[]) {

FILE *fptr;

char filename[100], ch;

char *substring;

int count = 0;

if (argc != 3) {

printf("Usage: %s filename substring\n", argv[0]);

exit(EXIT_FAILURE);

strcpy(filename, argv[1]);

substring = argv[2];

if ((fptr = fopen(filename, "r")) == NULL) {

printf("Error opening file\n");

exit(EXIT_FAILURE);

while ((ch = fgetc(fptr)) != EOF) {

if (strstr(&ch, substring) != NULL) {

count++;

printf("The substring \"%s\" occurs %d times in the file \"%s\"\n", substring, count, filename);

fclose(fptr);

return 0;

You might also like