From c23e09cb9042c3c31b7c513f424318158f4d13a2 Mon Sep 17 00:00:00 2001 From: aadit Date: Mon, 5 Aug 2024 01:09:43 +0530 Subject: [PATCH] Upload files to "DS/C/Lab/Week2" --- DS/C/Lab/Week2/StringHandler.c | 119 ++++++++++++++++++++++++ DS/C/Lab/Week2/StringHandling.c | 139 ++++++++++++++++++++++++++++ DS/C/Lab/Week2/StructureStudent.c | 60 ++++++++++++ DS/C/Lab/Week2/StudentRecordsFile.c | 73 +++++++++++++++ DS/C/Lab/Week2/timehandler.c | 68 ++++++++++++++ 5 files changed, 459 insertions(+) create mode 100644 DS/C/Lab/Week2/StringHandler.c create mode 100644 DS/C/Lab/Week2/StringHandling.c create mode 100644 DS/C/Lab/Week2/StructureStudent.c create mode 100644 DS/C/Lab/Week2/StudentRecordsFile.c create mode 100644 DS/C/Lab/Week2/timehandler.c diff --git a/DS/C/Lab/Week2/StringHandler.c b/DS/C/Lab/Week2/StringHandler.c new file mode 100644 index 0000000..7b0bad5 --- /dev/null +++ b/DS/C/Lab/Week2/StringHandler.c @@ -0,0 +1,119 @@ +#include + +int string_length(char str[]) { + int length = 0; + while (str[length] != '\0') { + length++; + } + return length; +} + +void string_concatenate(char str1[], char str2[], char result[]) { + int i = 0, j = 0; + while (str1[i] != '\0') { + result[j++] = str1[i++]; + } + i = 0; + while (str2[i] != '\0') { + result[j++] = str2[i++]; + } + result[j] = '\0'; +} + +int string_compare(char str1[], char str2[]) { + int i = 0; + while (str1[i] != '\0' && str2[i] != '\0') { + if (str1[i] != str2[i]) { + return (str1[i] < str2[i]) ? -1 : 1; + } + i++; + } + if (str1[i] == '\0' && str2[i] == '\0') { + return 0; + } else if (str1[i] == '\0') { + return -1; + } else { + return 1; + } +} + +void string_insert(char str[], char substr[], int pos, char result[]) { + int i = 0, j = 0, k = 0; + int str_length = string_length(str); + int substr_length = string_length(substr); + + while (i < pos && i < str_length) { + result[j++] = str[i++]; + } + while (substr[k] != '\0') { + result[j++] = substr[k++]; + } + while (i < str_length) { + result[j++] = str[i++]; + } + result[j] = '\0'; +} + +void string_delete(char str[], int pos, int length, char result[]) { + int i = 0, j = 0; + int str_length = string_length(str); + + while (i < pos && i < str_length) { + result[j++] = str[i++]; + } + i += length; + while (i < str_length) { + result[j++] = str[i++]; + } + result[j] = '\0'; +} + +int main() { + char str1[100], str2[100], result[200]; + int choice, pos, length; + + printf("Enter the first string: "); + scanf("%99s", str1); + printf("Enter the second string: "); + scanf("%99s", str2); + + printf("\nChoose an operation:\n"); + printf("1. Length of the first string\n"); + printf("2. Concatenate strings\n"); + printf("3. Compare strings\n"); + printf("4. Insert second string into first string at position\n"); + printf("5. Delete substring from first string\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Length of the first string: %d\n", string_length(str1)); + break; + case 2: + string_concatenate(str1, str2, result); + printf("Concatenated string: %s\n", result); + break; + case 3: + printf("String comparison result: %d\n", string_compare(str1, str2)); + break; + case 4: + printf("Enter position to insert the second string: "); + scanf("%d", &pos); + string_insert(str1, str2, pos, result); + printf("String after insertion: %s\n", result); + break; + case 5: + printf("Enter position to start deletion: "); + scanf("%d", &pos); + printf("Enter length of substring to delete: "); + scanf("%d", &length); + string_delete(str1, pos, length, result); + printf("String after deletion: %s\n", result); + break; + default: + printf("Invalid choice.\n"); + } + + return 0; +} diff --git a/DS/C/Lab/Week2/StringHandling.c b/DS/C/Lab/Week2/StringHandling.c new file mode 100644 index 0000000..b5be671 --- /dev/null +++ b/DS/C/Lab/Week2/StringHandling.c @@ -0,0 +1,139 @@ +#include + +int string_length(char string[]) +{ + int i; + for(i=0; string[i]!='\0'; i++); + return i; +} + +void string_concat(char str1[], char str2[]) +{ + int i,j; + char str5[100]; + + for(i=0;str1[i]!='\0';i++) + str5[i]=str1[i]; + + str5[i] = '\0'; + + for(i=string_length(str5),j=0;str2[j]!='\0';i++,j++) + str5[i]=str2[j]; + + str5[i] = '\0'; + + printf("Joining Strings 1 and 2, we get: %s\n",str5); +} + +void string_comp(char str1[], char str2[]) +{ + int i; + + for(i=0;str1[i]!='\0' || str2[i]!='\0' ;i++){ + if(str1[i]>str2[i]){ + printf("The first string is greater than the second.\n"); + break; + }else if(str1[i] string_length(str1)){ + printf("Invalid Position."); + return; + + }else{ + + for(i=0;i= string_length(str1)) { + printf("Invalid Position.\n"); + return; + } else if (str3_len > string_length(str1 + pos)) { + printf("Deletion substring is too long.\n"); + return; + } else { + for (i = pos, j = 0; j < str3_len; i++, j++) { + if (str1[i] != str3[j]) { + printf("The substring doesn't match.\n"); + return; + } + } + for (i = pos; str1[i + str3_len] != '\0'; i++) { + str1[i] = str1[i + str3_len]; + } + str1[i] = '\0'; + } +} + +void main() +{ + int a,b; + char str1[100],str2[100],str3[100]; + + printf("Enter the first string: "); + gets(str1); + + printf("Enter the second string: "); + gets(str2); + + printf("The first string is %s. \n",str1); + printf("The second string is %s. \n",str2); + + // String Length + + printf("The length of String 1 is %d. \n",string_length(str1)); + printf("The length of String 2 is %d. \n",string_length(str2)); + + // String Concatenation + + string_concat(str1, str2); + + // String Comparison + + string_comp(str1, str2); + + // String Insertion + + printf("Enter the position at which you would like to insert string 2 into string 1: "); + scanf("%d",&a); + + string_insert(str1, str2, a); + + printf("Enter the position at which you would like to check for deletion substring: "); + scanf("%d",&b); + + printf("Enter the part of the string that you want to delete from string 1: "); + getchar(); // Clear the newline character from the input buffer + fgets(str3, sizeof(str3), stdin); // Use fgets instead of gets for safer input + + string_destroy(str1, str3, b); +} diff --git a/DS/C/Lab/Week2/StructureStudent.c b/DS/C/Lab/Week2/StructureStudent.c new file mode 100644 index 0000000..f370356 --- /dev/null +++ b/DS/C/Lab/Week2/StructureStudent.c @@ -0,0 +1,60 @@ +#include + +typedef struct { + char name[50]; + int roll_no; + char grade; +} student; + +void read_students(student students[], int n) { + for (int i = 0; i < n; i++) { + printf("Enter details for student %d:\n", i + 1); + printf("Name: "); + scanf("%s", students[i].name); + printf("Roll No: "); + scanf("%d", &students[i].roll_no); + printf("Grade: "); + scanf(" %c", &students[i].grade); + } +} + +void display_students(student students[], int n) { + printf("\nStudent Information:\n"); + for (int i = 0; i < n; i++) { + printf("Name: %s, Roll No: %d, Grade: %c\n", students[i].name, students[i].roll_no, students[i].grade); + } +} + +void sort_students_by_roll(student students[], int n) { + student temp; + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + if (students[i].roll_no > students[j].roll_no) { + temp = students[i]; + students[i] = students[j]; + students[j] = temp; + } + } + } +} + +int main() { + int n; + + printf("Enter the number of students: "); + scanf("%d", &n); + + student students[n]; + + read_students(students, n); + + printf("\nBefore sorting:\n"); + display_students(students, n); + + sort_students_by_roll(students, n); + + printf("\nAfter sorting by roll number:\n"); + display_students(students, n); + + return 0; +} diff --git a/DS/C/Lab/Week2/StudentRecordsFile.c b/DS/C/Lab/Week2/StudentRecordsFile.c new file mode 100644 index 0000000..d00ee6f --- /dev/null +++ b/DS/C/Lab/Week2/StudentRecordsFile.c @@ -0,0 +1,73 @@ +#include + +typedef struct { + char name[50]; + int roll_no; + char grade; + char branch[50]; +} Student; + +void write_student_records(const char filename[], Student students[], int n) { + FILE *file = fopen(filename, "w"); + if (file == NULL) { + printf("Error opening file.\n"); + return; + } + + for (int i = 0; i < n; i++) { + fprintf(file, "%s %d %c %s\n", students[i].name, students[i].roll_no, students[i].grade, students[i].branch); + } + + fclose(file); +} + +void store_branchwise_records(const char filename[]) { + FILE *file = fopen(filename, "r"); + if (file == NULL) { + printf("Error opening file.\n"); + return; + } + + Student student; + char branch_filename[50]; + + while (fscanf(file, "%s %d %c %s", student.name, &student.roll_no, &student.grade, student.branch) != EOF) { + sprintf(branch_filename, "%s.txt", student.branch); + FILE *branch_file = fopen(branch_filename, "a"); + if (branch_file == NULL) { + printf("Error opening branch file.\n"); + continue; + } + fprintf(branch_file, "%s %d %c\n", student.name, student.roll_no, student.grade); + fclose(branch_file); + } + + fclose(file); +} + +int main() { + int n; + printf("Enter the number of students: "); + scanf("%d", &n); + + Student students[n]; + + for (int i = 0; i < n; i++) { + printf("Enter details for student %d:\n", i + 1); + printf("Name: "); + scanf("%s", students[i].name); + printf("Roll No: "); + scanf("%d", &students[i].roll_no); + printf("Grade: "); + scanf(" %c", &students[i].grade); + printf("Branch: "); + scanf("%s", students[i].branch); + } + + write_student_records("students.txt", students, n); + store_branchwise_records("students.txt"); + + printf("Records stored branch-wise in separate files.\n"); + + return 0; +} diff --git a/DS/C/Lab/Week2/timehandler.c b/DS/C/Lab/Week2/timehandler.c new file mode 100644 index 0000000..9fc1677 --- /dev/null +++ b/DS/C/Lab/Week2/timehandler.c @@ -0,0 +1,68 @@ +#include + +typedef struct { + int hour; + int min; + int sec; +} Time; + +void read_time(Time *t) { + scanf("%d %d %d", &t->hour, &t->min, &t->sec); +} + +void display_time(Time t) { + printf("%02d:%02d:%02d\n", t.hour, t.min, t.sec); +} + +Time add_time(Time t1, Time t2) { + Time result; + result.sec = t1.sec + t2.sec; + result.min = t1.min + t2.min + result.sec / 60; + result.sec %= 60; + result.hour = t1.hour + t2.hour + result.min / 60; + result.min %= 60; + result.hour %= 24; + return result; +} + +Time diff_time(Time t1, Time t2) { + Time result; + int total_sec1 = t1.hour * 3600 + t1.min * 60 + t1.sec; + int total_sec2 = t2.hour * 3600 + t2.min * 60 + t2.sec; + int diff_sec = total_sec1 - total_sec2; + + if (diff_sec < 0) { + diff_sec += 24 * 3600; + } + + result.hour = diff_sec / 3600; + result.min = (diff_sec % 3600) / 60; + result.sec = diff_sec % 60; + return result; +} + +int main() { + Time t1, t2, sum, difference; + + printf("Enter the first time (hours minutes seconds): "); + read_time(&t1); + + printf("Enter the second time (hours minutes seconds): "); + read_time(&t2); + + printf("First time: "); + display_time(t1); + + printf("Second time: "); + display_time(t2); + + sum = add_time(t1, t2); + printf("Sum of times: "); + display_time(sum); + + difference = diff_time(t1, t2); + printf("Difference between times: "); + display_time(difference); + + return 0; +}