[go: up one dir, main page]

0% found this document useful (0 votes)
13 views7 pages

Ex. No: 7 Generating A Transcript With CGPA and GRADE: Program

The document outlines two C programs: one for generating a student transcript with CGPA and grade, and another for performing file operations such as counting characters and replacing words in a file. The first program defines a Student structure, calculates CGPA based on input marks, and displays the transcript. The second program includes functionalities to count characters in a file and replace specific words, demonstrating file handling in C.

Uploaded by

bharanibeast7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Ex. No: 7 Generating A Transcript With CGPA and GRADE: Program

The document outlines two C programs: one for generating a student transcript with CGPA and grade, and another for performing file operations such as counting characters and replacing words in a file. The first program defines a Student structure, calculates CGPA based on input marks, and displays the transcript. The second program includes functionalities to count characters in a file and replace specific words, demonstrating file handling in C.

Uploaded by

bharanibeast7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.

No: 7 Generating a transcript with CGPA and GRADE

AIM: To write a C program that generating a transcript with CGPA and GRADE
ALGORITHM:
Step 1: Start.
Step 2: Define a structure Student with:name (array of characters to store the
student's name),roll_no (integer to store the roll number),marks[5] (array of 5
floating-point numbers to store marks for 5 subjects),CGPA (floating-point
number to store the calculated CGPA),grade (character to store the calculated
grade)
Step 3: Input Student Details:Prompt the user to enter the student's name.
Use fgets to read the input string, including spaces.Remove any newline
character at the end of the name using strcspn.Prompt the user to enter
the student's roll number.Prompt the user to enter marks for 5 subjects
(assume marks are out of 10).For each subject, read the floating-point
number representing the marks.
Step 4: Calculate CGPA and Grade:Initialize a variable total_marks to 0.
Loop through the marks array (5 subjects) and sum up all the marks.
Calculate the CGPA as total_marks / 50 (since each subject is out of 10 and there are 5
subjects).Determine the grade based on the CGPA:
If CGPA >= 9, assign grade 'A'.
If CGPA >= 8, assign grade 'B'.
If CGPA >= 7, assign grade 'C'.
If CGPA >= 6, assign grade 'D'.
Otherwise, assign grade 'F'.
Step 5: Display Transcript:
Print the student's name.
Print the student's roll number.
Print the marks for each of the 5 subjects.
Print the CGPA.
Print the grade.
Step 6: Stop.

PROGRAM:
#include <stdio.h>

// Define the structure for a student


struct Student {
char name[50];
int roll_no;
float marks[5]; // Array to store marks for 5 subjects
float CGPA;
char grade;
};

// Function to calculate CGPA and grade


void calculateCGPAandGrade(struct Student* s) {
float total_marks = 0;
// Calculate total marks
for (int i = 0; i < 5; i++) {
total_marks += s->marks[i];
}

// Calculate CGPA (out of 10)


s->CGPA = total_marks / 50; // assuming each subject is out of 10

// Determine the grade based on CGPA


if (s->CGPA >= 9) {
s->grade = 'A';
} else if (s->CGPA >= 8) {
s->grade = 'B';
} else if (s->CGPA >= 7) {
s->grade = 'C';
} else if (s->CGPA >= 6) {
s->grade = 'D';
} else {
s->grade = 'F';
}
}

// Function to display the transcript


void displayTranscript(struct Student* s) {
printf("\n--- Transcript ---\n");
printf("Name: %s\n", s->name);
printf("Roll Number: %d\n", s->roll_no);
printf("Marks: ");
for (int i = 0; i < 5; i++) {
printf("%.2f ", s->marks[i]);
}
printf("\nCGPA: %.2f\n", s->CGPA);
printf("Grade: %c\n", s->grade);
}

int main() {
struct Student student;

// Input student details


printf("Enter the student's name: ");
fgets(student.name, sizeof(student.name), stdin);

// Remove newline character that fgets might have added


student.name[strcspn(student.name, "\n")] = '\0';

printf("Enter the student's roll number: ");


scanf("%d", &student.roll_no);

// Input marks for 5 subjects


printf("Enter marks for 5 subjects (out of 10): \n");
for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &student.marks[i]);
}

// Calculate CGPA and Grade


calculateCGPAandGrade(&student);

// Display the transcript


displayTranscript(&student);

return 0;
}

O/P:
Enter the student's name: John Doe
Enter the student's roll number: 101
Enter marks for 5 subjects (out of 10):
Subject 1: 8
Subject 2: 9
Subject 3: 7
Subject 4: 6
Subject 5: 8

--- Transcript ---


Name: John Doe
Roll Number: 101
Marks: 8.00 9.00 7.00 6.00 8.00
CGPA: 7.60
Grade: C

RESULT:
Thus, the C program that generating a transcript with CGPA and GRADE
EX.NO:8 Demonstrate file operations
8a. Count number of characters in the file
AIM:
To write a C program to Count number of characters in the file.
ALGORITHM:

Step 1: Start.
Step 2: Input the filename.
Step 3:Open the file for reading (fopen(filename, "r")).
Step 4:If the file can't be opened, print an error message and End.
Step 5:If the file is opened successfully, read the file character by character using getc.
Step 6:Increment the character count for each character read.
Step 7:When the end of the file (EOF) is reached, close the file (fclose(fp)).
Step 8:Print the total count of characters in the file.
Step 9: Stop.

PROGRAM:

#include<stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE* fp;
int count = 0;
char filename[MAX_FILE_NAME];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not open file %s",filename);
return 0;
}
for (c = getc(fp); c != EOF; c = getc(fp))
count = count + 1;
fclose(fp);
printf("The file %s has %d characters\n ",filename, count);
return 0;
}
Output:
Enter file name: Empdetails.txt
The file Empdetails.txt has 26 characters
RESULT:
Thus, the C program for Counting the number of characters in the file has been executed and
verified successfully.
8b. Replace a specific word with the given word in the same file
AIM:
To write a C program to Replace a specific word with the given word in
the same file
ALGORITHM:

Step 1: Start.
Step 2: nput filename, oldWord, newWord.
Step 3: Open the file for reading and the temporary file for writing Step
Step 4: Read the original file line by line.
For each line:
Search for occurrences of oldWord.
Step 5: If oldWord is found, replace it with newWord in the temporary file.
Step 6: If oldWord is not found, copy the character from the original file to the temporary file.
Close the original file and the temporary file.
Step 7: Delete the original file and rename the temporary file to the original filename.
Step 8: Print a success message

PROGRAM:

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

#define MAX_LENGTH 1000

// Function to replace all occurrences of 'oldWord' with 'newWord' in a file


void replaceWordInFile(FILE *file, const char *oldWord, const char *newWord) {
FILE *tempFile;
char buffer[MAX_LENGTH];
char temp[MAX_LENGTH];
int i, j, found;

// Open a temporary file to store the modified content


tempFile = fopen("temp.txt", "w");
if (tempFile == NULL) {
printf("Unable to open temporary file.\n");
return;
}

// Read the content of the original file line by line


while (fgets(buffer, sizeof(buffer), file) != NULL) {
i = 0;
while (buffer[i] != '\0') {
found = 1;
j = 0;
// Check if the current part of the line matches the 'oldWord'
while (oldWord[j] != '\0' && buffer[i + j] == oldWord[j]) {
j++;
}

// If the 'oldWord' is found, replace it with the 'newWord'


if (oldWord[j] == '\0' && (buffer[i + j] == ' ' || buffer[i + j] == '\0' || buffer[i + j] == '\n')) {
// Copy new word to temp
fprintf(tempFile, "%s", newWord);
i += j; // Move past the old word
} else {
// Copy the current character from buffer to temp
fputc(buffer[i], tempFile);
i++;
}
}
}

// Close the temporary file


fclose(tempFile);

// Close the original file


fclose(file);

// Delete the original file


remove("input.txt");

// Rename the temporary file to the original file name


rename("temp.txt", "input.txt");

printf("The word replacement is done successfully.\n");


}

int main() {
FILE *file;
char filename[100];
char oldWord[100], newWord[100];

// Input file name, word to replace and new word


printf("Enter the filename: ");
scanf("%s", filename);
printf("Enter the word to be replaced: ");
scanf("%s", oldWord);
printf("Enter the new word: ");
scanf("%s", newWord);

// Open the file for reading


file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}

// Replace the word in the file


replaceWordInFile(file, oldWord, newWord);

return 0;
}

O/P:
Hello world!
This is a test file. Hello again!

Hi world!
This is a test file. Hi again!

Enter the filename: input.txt


Enter the word to be replaced: Hello
Enter the new word: Hi

The word replacement is done successfully.

RESULT:
Thus, the C program for Replacing a specific word with the given word in the file has been
executed and verified successfully.

You might also like