0% found this document useful (0 votes)
5 views

C Programs

Tricky C programs
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)
5 views

C Programs

Tricky C programs
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/ 4

C Programming Solutions

9) Program to Print Occurrences of Alphabets in Text (Command Line


Arguments)

#include <stdio.h>
#include <ctype.h>

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


int count[26] = {0}; // Array to hold count of each alphabet

// Traverse through command line arguments


for (int i = 1; i < argc; i++) {
for (int j = 0; argv[i][j] != '\0'; j++) {
char ch = tolower(argv[i][j]); // Convert to lowercase
if (ch >= 'a' && ch <= 'z') {
count[ch - 'a']++; // Update count for each alphabet
}
}
}

// Print the occurrences of each alphabet


printf("Occurrences of alphabets:\n");
for (int i = 0; i < 26; i++) {
if (count[i] > 0) {
printf("%c: %d\n", i + 'a', count[i]);
}
}

return 0;
}

18) Program to Store Student Information in a File

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

struct Student {
int roll_no;
char name[50];
char class[10];
int year;
float total_marks;
};

int main() {
FILE *file;
struct Student students[10];

// Input student data


for (int i = 0; i < 10; i++) {
printf("Enter details for student %d\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].roll_no);
printf("Name: ");
scanf(" %[^
]%*c", students[i].name);
printf("Class: ");
scanf("%s", students[i].class);
printf("Year: ");
scanf("%d", &students[i].year);
printf("Total Marks: ");
scanf("%f", &students[i].total_marks);
}

// Open file in write mode


file = fopen("students.dat", "wb");
if (file == NULL) {
printf("Error opening file!\n");
exit(1);
}

// Write student data to file


fwrite(students, sizeof(struct Student), 10, file);
fclose(file);

printf("Student data saved successfully.\n");

return 0;
}
19) Program to Retrieve and Print Student Information from File

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

struct Student {
int roll_no;
char name[50];
float total_marks;
};

int main() {
FILE *file;
struct Student students[10];

// Open file in read mode


file = fopen("students.dat", "rb");
if (file == NULL) {
printf("Error opening file!\n");
exit(1);
}

// Read student data from file


fread(students, sizeof(struct Student), 10, file);
fclose(file);

// Print student data in required format


printf("Roll No.\tName\tMarks\n");
for (int i = 0; i < 10; i++) {
printf("%d\t%s\t%.2f\n", students[i].roll_no, students[i].name,
students[i].total_marks);
}

return 0;
}

20) Program to Copy Contents of One File to Another (Remove


Whitespaces)

#include <stdio.h>
#include <ctype.h>
int main() {
FILE *sourceFile, *targetFile;
char ch;

// Open source file in read mode


sourceFile = fopen("source.txt", "r");
if (sourceFile == NULL) {
printf("Error opening source file!\n");
return 1;
}

// Open target file in write mode


targetFile = fopen("target.txt", "w");
if (targetFile == NULL) {
printf("Error opening target file!\n");
return 1;
}

// Copy contents, skipping whitespaces


while ((ch = fgetc(sourceFile)) != EOF) {
if (!isspace(ch)) {
fputc(ch, targetFile);
}
}

printf("File copied successfully after removing whitespaces.\n");

fclose(sourceFile);
fclose(targetFile);

return 0;
}

You might also like