Part A
1. Develop a program in C for operations on strings like finding the string of length
copying two strings comparing two strings and pattern matching and replacing support
the program with function for each of the above operation don't use built in functions
#include <stdio.h>
// Function to find the length of a string
int string_length(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
return length;
// Function to compare two strings
int string_compare(char str1[], char str2[]) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return 0; // Strings are not equal
i++;
if (str1[i] == '\0' && str2[i] == '\0') {
return 1; // Strings are equal
return 0; // Strings are not equal
}
// Function to copy one string to another
void string_copy(char source[], char destination[]) {
int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
destination[i] = '\0'; // Null terminate the destination string
// Function to perform pattern matching (search for a substring in a string)
int string_match(char str[], char pattern[]) {
int i, j;
int n = string_length(str);
int m = string_length(pattern);
// Loop over the main string
for (i = 0; i <= n - m; i++) {
// Check for a match of the pattern
for (j = 0; j < m; j++) {
if (str[i + j] != pattern[j]) {
break;
if (j == m) { // Full match found
return i; // Return starting index of the match
}
}
return -1; // No match found
int main() {
char str1[100], str2[100], pattern[100];
// Input strings
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
// Remove newline character added by fgets
str1[string_length(str1) - 1] = '\0';
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
str2[string_length(str2) - 1] = '\0';
printf("Enter pattern to search: ");
fgets(pattern, sizeof(pattern), stdin);
pattern[string_length(pattern) - 1] = '\0';
// Test string length function
printf("Length of first string: %d\n", string_length(str1));
printf("Length of second string: %d\n", string_length(str2));
// Test string compare function
if (string_compare(str1, str2)) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
// Test string copy function
char copied_string[100];
string_copy(str1, copied_string);
printf("Copied string: %s\n", copied_string);
// Test pattern matching function
int match_index = string_match(str1, pattern);
if (match_index != -1) {
printf("Pattern found at index %d.\n", match_index);
} else {
printf("Pattern not found.\n");
return 0;
2. write a C program to read the name and roll number of n students from the user and
store this data in a file.
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int rollNumber;
};
int main() {
FILE *file;
int n;
// Ask the user for the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Open the file in write mode
file = fopen("students.txt", "w");
if (file == NULL) {
printf("Error opening the file!\n");
return 1;
// Declare a variable of type struct Student
struct Student student;
// Read details of each student and write to the file
for (int i = 0; i < n; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", student.name); // Using scanf for name input
printf("Enter roll number of student %d: ", i + 1);
scanf("%d", &student.rollNumber);
// Write student data to the file
fprintf(file, "Student %d: Name = %s, Roll Number = %d\n", i + 1, student.name,
student.rollNumber);
// Close the file
fclose(file);
printf("Student data has been written to the file.\n");
return 0;