[go: up one dir, main page]

0% found this document useful (0 votes)
19 views11 pages

Assignment 1

The document contains multiple C programming assignments that cover various file operations, including counting characters, words, and lines in a file, reading and storing student names and marks, deleting a specific line from a file, copying content between files, detecting tokens in a C program, and removing stop words from a text file. Each assignment includes code snippets and example outputs demonstrating the functionality. The programs are designed to enhance understanding of file handling and string manipulation in C.

Uploaded by

Nehal Srivastava
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)
19 views11 pages

Assignment 1

The document contains multiple C programming assignments that cover various file operations, including counting characters, words, and lines in a file, reading and storing student names and marks, deleting a specific line from a file, copying content between files, detecting tokens in a C program, and removing stop words from a text file. Each assignment includes code snippets and example outputs demonstrating the functionality. The programs are designed to enhance understanding of file handling and string manipulation in C.

Uploaded by

Nehal Srivastava
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/ 11

Assignment No.

-1

Question 1:- Write a C program to scan and count the number of characters,words,and lines in a file.

Code:-
#include<stdio.h>
int main(){
FILE *file;
char filename[100];
char ch;
int characters=0,words=0,lines=0;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename,"r");
if(file == NULL){
printf("Could not open file.\n");
return 1;
}
while((ch = fgetc(file)) != EOF){
characters++;
if(ch == ' ' || ch == '\n')
words++;
if(ch == '\n')
lines++;
}
fclose(file);
printf("Characters: %d\n",characters);
printf("Words: %d\n",words+1);
printf("Lines: %d\n",lines);
return 0;
}

Output:-
story.txt file:-
Karen Saxby is the author of the Storyfun series, published by Cambridge University Press. She
also co-wrote the Fun For series, and is an experienced Cambridge English consultant. In this
article, she explores how stories can be used to make young people's language learning meaningful
and memorable.

Question 2:- Write a C program to read names and marks of n number of students from users and
store them in a file.

Code:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student {
char name[50];
int marks;
};

int main() {
int n, i;
printf("Enter the number of students: ");
scanf("%d", &n);

struct Student students[n];

FILE *file = fopen("students.txt", "w");

if (file == NULL) {
printf("Error opening file!\n");
return 1;
}

for (i = 0; i < n; i++) {


printf("\nEnter name of student %d: ", i + 1);
getchar();
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = 0;

printf("Enter marks of student %d: ", i + 1);


scanf("%d", &students[i].marks);

fprintf(file, "Name: %s, Marks: %d\n", students[i].name, students[i].marks);


}

printf("\nStudent information has been saved to 'students.txt'.\n");

fclose(file);
return 0;
}

Output:-

student.txt file:-
Name: Ravi, Marks: 990
Name: Utsav, Marks: 360

Question 3:- write a C program Delete a specific Line from a text file.

Code:-
#include <stdio.h>
#include <stdlib.h>

int main() {
char filename[100];
int lineToDelete, lineNumber = 1;
char buffer[1024];

printf("Enter the filename: ");


scanf("%s", filename);

FILE *file = fopen(filename, "r");


if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter the line number to delete: ");
scanf("%d", &lineToDelete);

FILE *tempFile = fopen("temp.txt", "w");


if (tempFile == NULL) {
printf("Error creating temporary file!\n");
fclose(file);
return 1;
}

while (fgets(buffer, sizeof(buffer), file)) {


if (lineNumber != lineToDelete) {
fputs(buffer, tempFile);
}
lineNumber++;
}

fclose(file);
fclose(tempFile);

remove(filename);
rename("temp.txt", filename);

printf("Line %d has been deleted from the file.\n", lineToDelete);

return 0;
}

Output:-

File Before:-
File After:-

Question 4:- write a C program copy content from one file to another.

Code:-
#include <stdio.h>
#include <stdlib.h>

int main() {
char sourceFile[100], destFile[100];
char buffer[1024];
size_t bytesRead;

printf("Enter the source filename: ");


scanf("%s", sourceFile);

printf("Enter the destination filename: ");


scanf("%s", destFile);
FILE *src = fopen(sourceFile, "r");
if (src == NULL) {
printf("Error opening source file!\n");
return 1;
}

FILE *dest = fopen(destFile, "w");


if (dest == NULL) {
printf("Error opening destination file!\n");
fclose(src);
return 1;
}

while ((bytesRead = fread(buffer, 1, sizeof(buffer), src)) > 0) {


fwrite(buffer, 1, bytesRead, dest);
}

printf("Content copied from '%s' to '%s'.\n", sourceFile, destFile);

fclose(src);
fclose(dest);

return 0;
}

Output:-

Question 5:- write a C program to detect tokens in a C program.

Code:-
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_LEN 100


int isKeyword(const char *word) {
const char *keywords[] = {"int", "float", "double", "char", "if", "else", "while", "for", "return",
"break", "continue"};
for (int i = 0; i < 11; i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1;
}
}
return 0;
}

int isIdentifier(const char *word) {


if (!isalpha(word[0]) && word[0] != '_') {
return 0;
}
for (int i = 1; i < strlen(word); i++) {
if (!isalnum(word[i]) && word[i] != '_') {
return 0;
}
}
return 1;
}

int isNumber(const char *word) {


for (int i = 0; i < strlen(word); i++) {
if (!isdigit(word[i])) {
return 0;
}
}
return 1;
}

int main() {
char code[MAX_LEN];
char token[MAX_LEN];
int i = 0, j = 0;

printf("Enter a C program (end with # to stop):\n");


fgets(code, sizeof(code), stdin);

while (i < strlen(code)) {


if (isspace(code[i])) {
i++;
continue;
}

if (isalpha(code[i]) || code[i] == '_') {


j = 0;
while (isalnum(code[i]) || code[i] == '_') {
token[j++] = code[i++];
}
token[j] = '\0';
if (isKeyword(token)) {
printf("Keyword: %s\n", token);
} else if (isIdentifier(token)) {
printf("Identifier: %s\n", token);
}
}
else if (isdigit(code[i])) {
j = 0;
while (isdigit(code[i])) {
token[j++] = code[i++];
}
token[j] = '\0';
printf("Number: %s\n", token);
}
else if (code[i] == '+' || code[i] == '-' || code[i] == '*' || code[i] == '/' ||
code[i] == '=' || code[i] == '<' || code[i] == '>' || code[i] == '%' ||
code[i] == '!' || code[i] == '&' || code[i] == '|') {
token[0] = code[i++];
token[1] = '\0';
printf("Operator: %s\n", token);
}
else if (code[i] == '(' || code[i] == ')' || code[i] == '{' || code[i] == '}' ||
code[i] == '[' || code[i] == ']' || code[i] == ',' || code[i] == ';' ||
code[i] == '.') {
token[0] = code[i++];
token[1] = '\0';
printf("Punctuation: %s\n", token);
}
else {
i++;
}
}

return 0;
}

Output:-
Input:-
int main() {
int x = 10;
if (x > 5) {
printf("Hello, World!\n");
}
return 0;
}

Output:-
Keyword: int
Identifier: main
Punctuation: (
Punctuation: )
Punctuation: {
Keyword: int
Identifier: x
Operator: =
Number: 10
Punctuation: ;
Keyword: if
Punctuation: (
I dentifier: x
Operator: >
Number: 5
Punctuation: )
Punctuation: {
Identifier: printf
Punctuation: (
String: "Hello, World!
Punctuation: "
Punctuation: ;
Keyword: return
Number: 0
Punctuation: ;
Punctuation: }
Punctuation: }

Question 6:-write a C program takes two text files "stop_words.txt" and "story.txt" file by matching
with "stop_words.txt". After removing all stop word create a file called
"story_without_stopwords.txt",which will contain sentences without any stop word.

Code:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_LINE_LENGTH 1024


#define MAX_WORD_LENGTH 100

int isStopWord(const char *word, const char stopWords[][MAX_WORD_LENGTH], int


stopWordCount) {
for (int i = 0; i < stopWordCount; i++) {
if (strcmp(word, stopWords[i]) == 0) {
return 1;
}
}
return 0;
}

void removeStopWords(FILE *storyFile, FILE *outputFile, const char stopWords[]


[MAX_WORD_LENGTH], int stopWordCount) {
char line[MAX_LINE_LENGTH];
char word[MAX_WORD_LENGTH];
while (fgets(line, sizeof(line), storyFile)) {
int i = 0, j = 0;
while (line[i] != '\0') {
// Read word by word
if (isalnum(line[i])) {
word[j++] = tolower(line[i]);
} else {
word[j] = '\0';
if (j > 0 && !isStopWord(word, stopWords, stopWordCount)) {
fprintf(outputFile, "%s ", word);
}
j = 0;
if (line[i] == '.' || line[i] == '!' || line[i] == '?' || line[i] == '\n') {
fprintf(outputFile, "%c", line[i]);
}
}
i++;
}
}
}

int main() {
FILE *stopWordsFile = fopen("stop_words.txt", "r");
FILE *storyFile = fopen("story.txt", "r");
FILE *outputFile = fopen("story_without_stopwords.txt", "w");

if (stopWordsFile == NULL || storyFile == NULL || outputFile == NULL) {


printf("Error opening file(s)!\n");
return 1;
}

char stopWords[1000][MAX_WORD_LENGTH];
int stopWordCount = 0;

while (fgets(stopWords[stopWordCount], sizeof(stopWords[stopWordCount]), stopWordsFile)) {

stopWords[stopWordCount][strcspn(stopWords[stopWordCount], "\n")] = '\0';


stopWordCount++;
}

removeStopWords(storyFile, outputFile, stopWords, stopWordCount);

fclose(stopWordsFile);
fclose(storyFile);
fclose(outputFile);

printf("Story without stop words has been saved to 'story_without_stopwords.txt'.\n");

return 0;
}
Output:-

story.txt file:-
Karen Saxby is the author of the Storyfun series, published by Cambridge University Press. She
also co-wrote the Fun For series, and is an experienced Cambridge English consultant. In this
article, she explores how stories can be used to make young people's language learning meaningful
and memorable.

stop_words.txt:-
After years of hard work, Emily finally achieved her dream. She stood at the edge of the old bridge,
gazing at the calm river below. The sun was setting, casting a warm golden glow over the
landscape. It had been a long journey filled with obstacles and challenges, but she had persevered.
With each passing day, she grew stronger, wiser, and more determined. Now, as she took a deep
breath, she realized that this was just the beginning of a new chapter. The future seemed bright, full
of endless possibilities. Her heart was filled with hope and excitement.

story_without_stopwords.txt:-
karen saxby is the author of the storyfun series published by cambridge university press .she also co
wrote the fun for series and is an experienced cambridge english consultant .in this article she
explores how stories can be used to make young people s language learning meaningful and
memorable .

You might also like