Assignment 1
Assignment 1
-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);
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
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];
fclose(file);
fclose(tempFile);
remove(filename);
rename("temp.txt", filename);
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;
fclose(src);
fclose(dest);
return 0;
}
Output:-
Code:-
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main() {
char code[MAX_LEN];
char token[MAX_LEN];
int i = 0, j = 0;
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>
int main() {
FILE *stopWordsFile = fopen("stop_words.txt", "r");
FILE *storyFile = fopen("story.txt", "r");
FILE *outputFile = fopen("story_without_stopwords.txt", "w");
char stopWords[1000][MAX_WORD_LENGTH];
int stopWordCount = 0;
fclose(stopWordsFile);
fclose(storyFile);
fclose(outputFile);
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 .