SE , CN, CD files
SE , CN, CD files
#include <stdio.h>
#include <string.h>
int main() {
int i, count = 0, j = 0;
strlen(data); i++) {
if(data[i] == '1') {
count++;
stuffedData[j++] = data[i];
} else {
count = 0;
stuffedData[j++] = data[i];
if(count == 5) {
count = 0;
stuffedData[j++] = '0';
INPUT/OUTPUT:
Enter the data: 11011111
Data after bit stuffing: 110111110
EXPERIMENT-2
AIM: Implement Byte Stuffing.
CODE:
#include <stdio.h>
#include <string.h>
void main(){
strcpy(esc, "esc");
int i, j, k = 0, n;
strcpy(frame[k++], "flag");
gets(str[i]);
strcpy(frame[k++], str[i]);
else{
strcpy(frame[k++], "esc");
strcpy(frame[k++], str[i]);
}
strcpy(frame[k++], "flag");
printf("------------------------------\n\n");
printf("%s\t", frame[i]);
INPUT/OUTPUT:
------------------------------
flag 1 1 0 0 0 0 1 0 flag
EXPERIMENT-3
Aim: Implement CRC Code.
CODE:
#include<stdio.h>
#include<string.h> #define
N strlen(gen_poly) char
data[28]; char
check_value[28]; char
gen_poly[10]; int
data_length,i,j; void
XOR(){
check_value[j] == gen_poly[j])?'0':'1');
data);
crc();
if(i<N-1) printf("\nError
detected\n\n");
else
void crc(){
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
if(check_value[0]=='1')
XOR();
for(j=0;j<N-1;j++)
check_value[j]=check_value[j+1];
check_value[j]=data[i++];
}while(i<=data_length+N-1);
transmitted: ");
scanf("%s",data);
printf("\n Enter the Generating polynomial: ");
scanf("%s",gen_poly); data_length=strlen(data);
for(i=data_length;i<data_length+N-1;i++)
data[i]='0';
printf("\n Data padded with n-1 zeros : %s",data);
crc();
printf("\nCRC or Check value is :
%s",check_value);
for(i=data_length;i<data_length+N-1;i++)
data[i]=check_value[i-data_length]; printf("\n
Final data to be sent : %s",data);
receiver();
return 0;
}
INPUT/OUTPUT:
Enter data to be transmitted: 1001101
Enter the Generating polynomial: 1011
Data padded with n-1 zeros : 1001101000
CRC or Check value is : 101
Final data to be sent : 1001101101
Enter the received data: 1001101101
Data received: 1001101101
No error detected
In case of error-
Enter the received data: 1001001101
Data received: 1001001101
Error detected
EXPERIMENT-4
AIM : Implement Hamming Code.
CODE:
#include<stdio.h>
#include <math.h>
int input[32];
int code[32];
int ham_calc(int,int); void
main(){ int n,i,p_n =
0,c_l,j,k;
printf("Please enter the length of the Data Word: ");
scanf("%d",&n);
printf("Please enter the Data Word:\n");
for(i=0;i<n;i++)
scanf("%d",&input[i]);
i=0;
while(n>(int)pow(2,i)-(i+1)){
p_n++;
i++;} c_l =
p_n + n; j=k=0;
for(i=0;i<c_l;i++){
if(i==((int)pow(2,k)-1)){
code[i]=0;
k++;}
else{
code[i]=input[j];
j++;}}
for(i=0;i<p_n;i++){
int position = (int)pow(2,i);
int value = ham_calc(position,c_l);
code[position-1]=value; }
printf("\nThe calculated Code Word is: ");
for(i=0;i<c_l;i++)
printf("%d",code[i]); printf("\n");
printf("Please enter the received Code Word:\n");
for(i=0;i<c_l;i++)
scanf("%d",&code[i]);
int error_pos = 0;
for(i=0;i<p_n;i++){
int position = (int)pow(2,i);
int value = ham_calc(position,c_l);
if(value != 0)
error_pos+=position;}
if(error_pos == 1)
printf("The received Code Word is correct.\n");
else
printf("Error at bit position: %d\n",error_pos);
}
int ham_calc(int position,int c_l){
int count=0,i,j;
i=position-1; while(i<c_l){
for(j=i;j<i+position;j++){
if(code[j] == 1)
count++;}
i=i+2*position;
}
if(count%2 ==0)
return 0;
else return 1;
}
INPUT/OUTPUT:
Please enter the length of the Data Word: 8 Please enter
the Data Word:
1
1
0
1
0
0
1
1
The calculated Code Word is: 011110100011 Please
enter the received Code Word:
0
1
1
1
1
1
1
0
0
0
1
1
Error at bit position: 6
EXPERIEMENT – 05
AIM: Write a program to classify an IP address into classes.
CODE:
#include <stdio.h>
}
octet = atoi(token); if (octet
>= 0 && octet <= 127)
{ printf("Class
A\n");
}
{ printf("Class
B\n");
}
{ printf("Class
C\n");
{ printf("Class D
(Multicast)\n");
}
if (ip[i] == '.')
dot_count++;
{
num_count++;
}
else {
if (dot_count != 3 || num_count == 0)
{ printf("Invalid IP address
format.\n"); return 1;
}
classify_ip_address(ip);
return 0;
}
OUTPUT :
Enter an IP address: 147.255.123.0
Class B
EXPERIEMENT- 06
AIM: Write a code to implement Sliding Wait Control.
CODE:
#include<stdio.h> int main() { int
w,i,f,frames[50]; printf("Enter window size: ");
scanf("%d",&w); printf("\nEnter number of
frames to transmit: "); scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++) scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by
the receiver\n\n",w); for(i=1;i<=f;i++)
{
if(i%w==0)
printf("%d ",frames[i]);
return 0;
OUTPUT:
Enter window size: 3
Enter 5 frames: 12 89 5 4 6
With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
12 89 5
46
int dist[50][50],temp[50][50],n,i,j,k,x;
void dvr(); int main() { printf("\nEnter
the number of nodes : ");
scanf("%d",&n); printf("\nEnter the
distance matrix :\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&dist[i][j]);
dist[i][i]=0;
temp[i][j]=j; }
printf("\n");
scanf("%d",&j);
scanf("%d",&x); dist[i][j]=x;
printf("After update\n\n");
dvr();
return 0;
} void
dvr() {
for (i = 0;
i < n;
i++)
{ dist[i][j] =
dist[i][k] + dist[k][j];
temp[i][j] = k;
for(i=0;i<n;i++)
printf("\n\n");
OUTPUT:
Enter the number of nodes : 4
12
9
16
20
18
15
32
41
24
51
1 3
enter the new cost
68
After update
--------------------------------
EXPERIEMENT- 08
AIM: Write a code to implement Flooding Algorithm.
CODE:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 int
graph[MAX][MAX];
int visited[MAX];
int nodes;
flood(i, current);
= 1;
OUTPUT:
Enter number of nodes: 4
0 1
0 3
1 9
Flooding started:
#include <limits.h>
int graph[MAX][MAX];
int distance[MAX]; int
visited[MAX]; int
parent[MAX]; int
nodes;
distance[start] = 0;
visited[u] = 1;
void printPath(int j) {
if (parent[j] == -1)
return;
printPath(parent[j]);
printf(" -> %d", j);
}
}
int main() { int edges;
printf("Enter number of nodes: ");
scanf("%d", &nodes);
return 0;
OUTPUT:
Enter number of nodes: 5
012
024
121
137
243
Aim: Write program to find ε – closure of all states of any given NFA with ε
transition.
#include <bits/stdc++.h>
int main(){
int n, m;
vector<vector<int>> e(n);
int u, v;
string sym;
if(sym == "eps")
e[u].push_back(v);
vector<set<int>> closure(n);
vector<bool> vis(n,false);
stack<int> st;
st.push(i);
vis[i] = true;
while(!st.empty()){
closure[i].insert(u);
for(int v : e[u]){
if(!vis[v]){
vis[v] = true;
st.push(v);
for(int s : closure[i]){
cout << s;
first = false;
return 0;
INPUT-
47
0 1 eps
0 2 eps
03a
1 2 eps
13b
2 3 eps
30c
OUTPUT-
E-closure(0) = {0,1,2,3}
E-closure(1) = {1,2,3}
E-closure(2) = {2,3}
E-closure(3) = {3}
Experiment 05:
int main(){
int n, m;
vector<vector<int>> eps(n);
vector<vector<pair<int,string>>> delta(n);
set<string> symbols;
else {
delta[u].push_back({v, sym});
symbols.insert(sym);
vector<set<int>> eClos(n);
stack<int> st;
st.push(i);
vis[i] = true;
while(!st.empty()){
eClos[i].insert(u);
for(int v : eps[u]){
if(!vis[v]){
vis[v] = true;
st.push(v);
vector<map<string,set<int>>> newDelta(n);
set<int> targets;
for(int q : eClos[i]){
if(p.second == sym){
for(int r : eClos[p.first])
targets.insert(r);
if(!targets.empty())
newDelta[i][sym] = move(targets);
}
}
cout << "δ(" << i << "," << kv.first << ") = {";
for(int s : kv.second){
cout << s;
first = false;
return 0;
INPUT-
47
0 1 eps
0 2 eps
03a
1 2 eps
13b
2 3 eps
30c
OUTPUT-
δ(0,a) = {3}
δ(0,b) = {3}
δ(0,c) = {0,1,2,3}
δ(1,b) = {3}
δ(1,c) = {0,1,2,3}
δ(2,c) = {0,1,2,3}
δ(3,c) = {0,1,2,3}
Experiment 06:
Aim: Write program to convert NFA to DFA.
#include <bits/stdc++.h>
using namespace std;
while (!stk.empty()) {
int u = stk.top(); stk.pop();
auto it = trans[u].find('e'); // 'e' for ε
if (it == trans[u].end()) continue;
for (int v : it->second) {
if (!closure.count(v)) {
closure.insert(v);
stk.push(v);
}
}
return closure;
}
set<int> moveStates(const set<int>& states, char a,
const vector<map<char, vector<int>>>& trans) {
set<int> result;
for (int s : states) {
auto it = trans[s].find(a);
if (it != trans[s].end()) {
result.insert(it->second.begin(), it->second.end());
}
}
return result;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<string> grid;
string line;
while (getline(cin, line)) {
if (line.empty()) break;
grid.push_back(line);
}
int N = grid.size();
vector<map<char, vector<int>>> trans(N);
set<char> alpha_set;
for (int i = 0; i < N; ++i) {
istringstream iss(grid[i]);
for (int j = 0; j < N; ++j) {
string cell;
iss >> cell;
if (cell != "-") {
char sym = cell[0];
trans[i][sym].push_back(j);
if (sym != 'e') alpha_set.insert(sym);
}
}
vector<char> alpha(alpha_set.begin(), alpha_set.end());
set<set<int>> dfaStates;
map<set<int>, set<int>> closures; // store ε-closures for printing
map<set<int>, char> name; // name DFA states A, B, ...
map<char, map<char, set<int>>> dfaTrans; // DFA transitions by set
queue<set<int>> q;
set<int> start_set = epsilonClosure({0}, trans);
dfaStates.insert(start_set);
closures[start_set] = start_set;
char nextName = 'A';
name[start_set] = nextName++;
q.push(start_set);
while (!q.empty()) {
auto T = q.front(); q.pop();
for (char a_sym : alpha) {
auto U = epsilonClosure(moveStates(T, a_sym, trans), trans);
if (U.empty()) continue;
if (!dfaStates.count(U)) {
dfaStates.insert(U);
closures[U] = U;
name[U] = nextName++;
q.push(U);
}
dfaTrans[name[T]][a_sym] = U;
}
cout << "The Epsilon Closures Are:\n";
for (int i = 0; i < N; ++i) {
set<int> single = epsilonClosure({i}, trans);
cout << "E(" << i << ")={";
for (int x : single) cout << x;
cout << "}\n";
}
cout << "\nDFA Table is:\n";
cout << "\t";
for (char a_sym : alpha) cout << a_sym << "\t";
cout << "\n";
vector<pair<char, set<int>>> order;
for (auto &p : name) order.emplace_back(p.second, p.first);
sort(order.begin(), order.end());
for (auto &pr : order) {
auto stSet = pr.second;
cout << "{";
for (int x : stSet) cout << x;
cout << "}\t";
for (char a_sym : alpha) {
if (dfaTrans[pr.first].count(a_sym)) {
auto &tgt = dfaTrans[pr.first][a_sym];
cout << "{";
for (int y : tgt) cout << y;
cout << "}";
} else {
cout << "{}";
}
cout << "\t";
}
cout << "\n";
}
return 0;
}
INPUT-
-ae-
--b-
---e
a---
OUTPUT-
The Epsilon Closures Are:
E(0)={023}
E(1)={1}
E(2)={23}
E(3)={3}
DFA Table is:
a b
{023} {0123} {}
{0123} {0123} {23}
{23} {023} {}
Experiment 07:
vector<char> symbols;
int dfa[MAX][MAX];
bool final_states[MAX];
bool distinguishable[MAX][MAX];
void initialize_distinguishable() {
if (final_states[i] != final_states[j])
distinguishable[i][j] = true;
else
distinguishable[i][j] = false;
void mark_distinguable_pairs() {
bool changed;
do {
changed = false;
if (distinguishable[i][j]) continue;
int i1 = dfa[i][k];
int j1 = dfa[j][k];
distinguishable[i][j] = true;
changed = true;
break;
} while (changed);
map<int,int> state_to_group;
for(int i=0;i<classes.size();i++) {
state_to_group[state]=i;
cout<<"State\t";
for(char sym: symbols) cout<<sym<<"\t";
cout<<"Final\n";
int rep=*classes[i].begin();
int next=dfa[rep][k];
cout<<"G"<<state_to_group[next]<<"\t";
bool is_final=false;
if (final_states[s]) {
is_final=true;
break;
void print_equivalence_classes() {
vector<set<int>> classes;
set<int> eq_class;
eq_class.insert(i);
grouped[i] = true;
if (i == j || grouped[j]) continue;
eq_class.insert(j);
grouped[j] = true;
classes.push_back(eq_class);
print_minimised_dfa(classes);
}
int main() {
symbols.resize(num_symbols);
cout << "Enter transition table (row: state, column: symbol index):\n";
int num_final;
int s;
cin >> s;
final_states[s] = true;
}
initialize_distinguishable();
mark_distinguable_pairs();
print_equivalence_classes();
return 0;
INPUT
4
01
01
10
23
32
23
OUTPUT
Enter number of states: Enter number of input symbols: Enter input symbols:
Enter transition table (row: state, column: symbol index):
Enter number of final states: Enter final states:
Minimized DFA State Groups:
Group 0: 0 1
Group 1: 2 3
const char terminals[] = {'+', '-', '*', '/', '(', ')', 'i', '$'};
char table[8][8] = {
{'>', '>', '>', '>', ' ', '>', ' ', '>'}, // )
{'>', '>', '>', '>', ' ', '>', ' ', '>'}, // i
};
int getIndex(char c) {
if (terminals[i] == c) return i;
return -1;
bool isOperator(char c) {
}
string preprocess(string input) {
string res;
return res;
void printStack(stack<char> s) {
vector<char> temp;
while (!s.empty()) {
temp.push_back(s.top());
s.pop();
stack<char> st;
st.push('$');
input = preprocess(input);
input += '$';
int i = 0;
char a = input[i];
char top;
while (!temp.empty()) {
if (c != 'E') {
top = c;
break;
printStack(st);
break;
i++;
a = input[i];
if (symbol == 'i') {
st.push('E');
if (st.top() == 'E') {
st.pop();
if (st.top() == '(') {
st.pop();
st.push('E');
st.pop();
st.push('E');
} else { cout << "Error\n"; break; }
} else {
break;
int main() {
string input;
parse(input);
return 0;
INPUT
(a+b)*c
OUTPUT
Stack Input Action
$ (i+i)*i$ Shift (
$( i+i)*i$ Shift i
$(i +i)*i$ Reduce id -> E
$(E +i)*i$ Shift +
$(E+ i)*i$ Shift i
$(E+i )*i$ Reduce id -> E
$(E+E )*i$ Reduce E + E -> E
$(E )*i$ Shift )
$(E) *i$ Reduce (E) -> E
$E *i$ Shift *
$E* i$ Shift i
$E*i $ Reduce id -> E
$E*E $ Reduce E * E -> E
$E $ Accept
Experiment 09:
Aim: Write program to find Simulate First and Follow of any given grammar.
#include <bits/stdc++.h>
set<char> nonTerminals;
char startSymbol;
bool isNonTerminal(char c) {
if (!isNonTerminal(symbol)) {
firstSet[symbol].insert(symbol);
return;
if (production == "e") {
firstSet[symbol].insert('e');
continue;
}
for (char ch : production) {
computeFirst(ch);
if (f != 'e')
firstSet[symbol].insert(f);
if (firstSet[ch].find('e') == firstSet[ch].end())
break;
if (firstSet[ch].find('e') == firstSet[ch].end()) {
allEpsilon = false;
break;
if (allEpsilon)
firstSet[symbol].insert('e');
set<char> result;
if (f != 'e')
result.insert(f);
if (firstSet[ch].find('e') == firstSet[ch].end())
return result;
result.insert('e');
return result;
if (symbol == startSymbol)
followSet[symbol].insert('$');
if (production[i] == symbol) {
if (i + 1 < production.length()) {
if (f != 'e')
followSet[symbol].insert(f);
}
if (firstNext.find('e') != firstNext.end()) {
if (lhs != symbol) {
computeFollow(lhs);
followSet[symbol].insert(f);
} else {
if (lhs != symbol) {
computeFollow(lhs);
followSet[symbol].insert(f);
int main() {
int n;
cin >> n;
if (i == 0) startSymbol = lhs;
nonTerminals.insert(lhs);
stringstream ss(rhs);
string prod;
grammar[lhs].push_back(prod);
computeFirst(nt);
computeFollow(nt);
if (!isNonTerminal(p.first)) continue;
return 0;
INPUT
3
S->AB
A->a|e
B->b
OUTPUT
First Sets:
FIRST(A) = { a e }
FIRST(B) = { b }
FIRST(S) = { a b }
Follow Sets:
FOLLOW(A) = { b }
FOLLOW(B) = { $ }
FOLLOW(S) = { $ }
Experiment 10:
Aim: Construct a recursive descent parser for an expression.
#include <iostream>
#include <string>
string input;
int pos = 0;
// Function declarations
pos++;
return true;
return false;
bool E() {
// E → T E'
if (T()) {
return E_();
return false;
}
bool E_() {
// E' → + T E' | ε
if (match('+')) {
if (T()) {
return E_();
return false;
return true; // ε
bool T() {
// T → F T'
if (F()) {
return T_();
return false;
bool T_() {
// T' → * F T' | ε
if (match('*')) {
if (F()) {
return T_();
}
return false;
return true; // ε
bool F() {
// F → (E) | i
if (match('i')) {
return true;
if (match('(')) {
return true;
return false;
return false;
int main() {
return 0;
INPUT
i+i*i
OUTPUT
Parsing successful!
Experiment 01
>>>Create a SRS document for e-banking system.
Software Requirements Specification (SRS) for E-Banking System
1. Introduction
1.1 Purpose
This document defines the software requirements for an E-Banking System, which allows customers
to perform banking transactions online securely and efficiently.
1.2 Scope
The E-Banking System will enable users to access their bank accounts, transfer funds, pay bills, check
balances, and manage personal information from a web-based or mobile platform.
1.4 References
2. Overall Description
• Account Management
• Fund Transfers
• Bill Payments
• Transaction History
• Customer Support
2.3 User Characteristics
The system will be used by customers, bank employees, and administrators. Customers may have
minimal technical knowledge, requiring an intuitive interface.
2.4 Constraints
• High-level security measures like encryption and multi-factor authentication are mandatory.
3. Specific Requirements
• Performance Optimization
4. Appendices
• User Guidelines
• Security Policy
• Legal Compliance
Conclusion:
This is the SRS document for e-banking system.
Experiment 02
>>>Draw the use case diagram and specify the role of each of the actors for
ATM Machine. Also state the precondition, postcondition and function of each
use case.
Use Case Diagram for ATM Machine
An automated teller machine (ATM) or the automatic banking machine (ABM) is a banking
subsystem (subject) that provides bank customers with access to financial transactions in a
public space without the need for a cashier, clerk, or bank teller.
Customer (actor) uses bank ATM to Check Balances of his/her bank accounts, Deposit Funds,
Withdraw Cash and/or Transfer Funds (use cases). ATM Technician provides Maintenance
and Repairs. All these use cases also involve Bank actor whether it is related to customer
transactions or to the ATM servicing.
Actors:
1. Customer – Uses the ATM to perform transactions.
2. Bank System – The backend system that verifies account details and processes
transactions.
3. Technician – Maintains and repairs the ATM.
Use Cases:
1. Authenticate User – Customer inserts a card and enters a PIN for verification.
1. Authenticate User
• Precondition: The ATM is powered on, and the user has an active bank card.
• Postcondition: The user is authenticated and can proceed with transactions.
• Function: Validate the card and PIN with the bank system.
2. Withdraw Cash
• Precondition: The user is authenticated, and the account has sufficient funds.
• Postcondition: Cash is dispensed, and the account balance is updated.
• Function: Deducts the amount from the account and dispenses cash.
3. Deposit Cash
4. Check Balance
5. Transfer Funds
6. Print Receipt
7. Maintain ATM
Conclusion:
This is the complete use case diagram for Bank ATM Machine.
Experiment 05
>>> Draw the sequence diagram for library management.
Sequence Diagram
A sequence diagram in UML (Unified Modelling Language) shows how objects interact with each
other in a particular time sequence. It’s particularly useful for understanding the flow of messages
and the order of operations in a system.
Conclusion:
This is the sequence diagram of Library Management System.
Experiment 06
>>Draw Collaboration diagram for Library management or banking management.
Definition:
A collaboration diagram, also called a communication diagram, emphasizes the interaction between
objects and the sequence of messages exchanged to complete a specific task.
Key Elements:
• Objects: User, Librarian, Book, Inventory System.
• Messages: Actions like requesting, checking availability, confirming, issuing, and updating
status.
Diagram:
Conclusion:
This is the collaboration diagram for library management system.
Experiment 07
>>Draw State Chart diagram for Library management or banking management.
Definition:
A state chart diagram represents the states of an object and the transitions between those states
triggered by events.
Key Elements:
• States: Available, Issued, Reserved, Lost, Returned.
• Entry and Exit Points: The diagram helps in tracking book availability and managing
transitions based on user interactions or system rules.
Diagram:
Conclusion:
Definition:
A component diagram depicts the organization and dependencies among a set of components in a
system.
Key Components:
• User Interface (UI): The front-end used by users and librarians.
Diagram:
Conclusion:
This is the Component diagram for library management system.
Activity diagram symbols: