Q26-WRITE A PROGRAM to Find the Length of a String
#include <stdio.h>
int stringLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
return length;
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int length = stringLength(str);
printf("The length of the string is: %d\n", length);
return 0;
OUTPUT:
Q27-WRITE A PROGRAM to Copy String using strcpy()
#include <stdio.h>
#include <string.h>
int main() {
char source[100];
char destination[100];
printf("Enter a string to copy: ");
fgets(source, sizeof(source), stdin);
source[strcspn(source, "\n")] = 0;
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
OUTPUT:
Q28-WRITE A PROGRAM to compare a string
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
return 0;
OUTPUT:
Q29-WRITE A PROGRAM to reverse a string
#include <stdio.h>
int main() {
char str[100];
char reversed[100];
int length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[length] != '\0') {
length++;
if (str[length - 1] == '\n') {
str[length - 1] = '\0';
length--;
for (int i = 0; i < length; i++) {
reversed[i] = str[length - 1 - i];
reversed[length] = '\0';
printf("Reversed string: %s\n", reversed);
return 0;
OUTPUT:
Q30-WRITE A PROGRAM to reverse a string
#include <stdio.h>
int main() {
char str[100];
char reversed[100];
int length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[length] != '\0') {
length++;
if (str[length - 1] == '\n') {
str[length - 1] = '\0';
length--;
for (int i = 0; i < length; i++) {
reversed[i] = str[length - 1 - i];
reversed[length] = '\0';
printf("Reversed string: %s\n", reversed);
return 0;
OUTPUT:
Q31- WRITE A PROGRAM to multiply two numbers using pointers.
#include <stdio.h>
void multiply(int *a, int *b, int *result) {
*result = (*a) * (*b);
int main() {
int num1, num2, product;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
multiply(&num1, &num2, &product);
printf("The product of %d and %d is: %d\n", num1, num2, product);
return 0;
OUTPUT:
Q32- WRITE A PROGRAM to display address of variable using pointers
#include <stdio.h>
int main() {
int num;
int *ptr;
num = 42;
ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", (void*)&num);
printf("Address of num using pointer: %p\n", (void*)ptr);
return 0;
OUTPUT:
Q33- WRITE A PROGRAM to show the memory occupied by Structure and
Union
#include <stdio.h>
struct MyStruct {
int intValue;
float floatValue;
char charValue;
};
union MyUnion {
int intValue;
float floatValue;
char charValue;
};
int main() {
struct MyStruct s;
union MyUnion u;
printf("Size of structure MyStruct: %zu bytes\n", sizeof(s));
printf("Size of union MyUnion: %zu bytes\n", sizeof(u));
return 0;
OUTPUT:
Q34- WRITE A PROGRAM to create Student I-Card using a Structure
#include <stdio.h>
#include <string.h>
struct StudentICard {
char name[50];
int rollNumber;
char course[50];
char department[50];
float cgpa;
};
void displayIcard(struct StudentICard student) {
printf("\n=========================\n");
printf(" I-CARD \n");
printf("=========================\n");
printf("Name: %s\n", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Course: %s\n", student.course);
printf("Department: %s\n", student.department);
printf("CGPA: %.2f\n", student.cgpa);
printf("=========================\n");
int main() {
struct StudentICard student;
printf("Enter student name: ");
fgets(student.name, sizeof(student.name), stdin);
student.name[strcspn(student.name, "\n")] = '\0';
printf("Enter roll number: ");
scanf("%d", &student.rollNumber);
printf("Enter course: ");
getchar();
fgets(student.course, sizeof(student.course), stdin);
student.course[strcspn(student.course, "\n")] = '\0';
printf("Enter department: ");
fgets(student.department, sizeof(student.department), stdin);
student.department[strcspn(student.department, "\n")] = '\0';
printf("Enter CGPA: ");
scanf("%f", &student.cgpa);
displayIcard(student);
return 0;
OUTPUT:
Q35- WRITE A PROGRAM to read data from a file from a file
#include <stdio.h>
#include <stdlib.h>
void readDataFromFile(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
int number;
int sum = 0;
int count = 0;
while (fscanf(file, "%d", &number) == 1) {
printf("Read number: %d\n", number);
sum += number;
count++;
fclose(file);
if (count > 0) {
printf("Total numbers read: %d\n", count);
printf("Sum of numbers: %d\n", sum);
} else {
printf("No numbers were read from the file.\n");
int main() {
const char *filename = "data.txt";
readDataFromFile(filename);
return 0;
}
OUTPUT:
Q36- WRITE A PROGRAM to save Employee details in a file using File
Handling.
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int id;
char department[50];
float salary;
};
void saveEmployeeDetails(const char *filename) {
FILE *file = fopen(filename, "a");
if (file == NULL) {
perror("Error opening file");
return;
struct Employee emp;
printf("Enter Employee Name: ");
scanf(" %[^\n]", emp.name);
printf("Enter Employee ID: ");
scanf("%d", &emp.id);
printf("Enter Department: ");
scanf(" %[^\n]", emp.department);
printf("Enter Salary: ");
scanf("%f", &emp.salary);
fprintf(file, "Name: %s\nID: %d\nDepartment: %s\nSalary: %.2f\n\n",
emp.name, emp.id, emp.department, emp.salary);
fclose(file);
printf("Employee details saved successfully!\n");
}
int main() {
const char *filename = "employees.txt";
char choice;
do {
saveEmployeeDetails(filename);
printf("Do you want to add another employee? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
return 0;
OUTPUT:
Q36-WRITE A PROGRAM to save Employee details in a file using File Handling
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int id;
char department[50];
float salary;
};
void saveEmployeeDetails(const char *filename) {
FILE *file = fopen(filename, "a");
if (file == NULL) {
perror("Error opening file");
return;
struct Employee emp;
printf("Enter Employee Name: ");
scanf(" %[^\n]", emp.name);
printf("Enter Employee ID: ");
scanf("%d", &emp.id);
printf("Enter Department: ");
scanf(" %[^\n]", emp.department);
printf("Enter Salary: ");
scanf("%f", &emp.salary);
fprintf(file, "Name: %s\nID: %d\nDepartment: %s\nSalary: %.2f\n\n",
emp.name, emp.id, emp.department, emp.salary);
fclose(file);
printf("Employee details saved successfully!\n");
}
int main() {
const char *filename = "employees.txt";
char choice;
do {
saveEmployeeDetails(filename);
printf("Do you want to add another employee? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
return 0;