Program for Character Stuffing:
import java.util.*;
class Char
{
public static void main(String r[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of characters: ");
int n=sc.nextInt();
String in[]=new String[n];
for(int i=0;i<n;i++)
{
in[i]=sc.next();
}
for(int i= 0;i<n;i++)
{
if(in[i].equals("dle"))
{
in[i]="dle dle";
}
}
System.out.println("Transmitted message is: ");
System.out.print(" dle stx ");
for(int i=0;i<n;i++)
{
System.out.print(in[i]+" ");
}
System.out.println(" dle etx ");
}
}
Example:
Sample output:
Enter number of characters:
5
a b dle dle d e
Transmitted message is:
dle stxa b dle dle dle dle d dle etx
Program for Bit Destuffing
//Removing a zero(0) after a series of 5 consecutive one's(1)
import java.util.*;
class BitDestuffing
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int []arr=new int[20];
int cnt=0;
int m=0;
System.out.print("Enter string length: ");
int a=input.nextInt();
System.out .print("Enter a string: ");
for(int i=0;i<a;i++)
{
arr[i]=input.nextInt();
}
for(int i=0;i<a;i++)
{
if(arr[i]==1)
cnt++;
else
cnt=0;
if(cnt==5)
{
if(arr[i+1]==0)
i=i++;
m=i+1;
cnt=0;
}
}
System.out.println("Your String is:");
for(int i=0;i<a;i++)
{
if(i==m)
{}
else
System.out.print(arr[i]);
}
}
}
import java.util.*;
class BitDestuffing
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int []arr=new int[20];
int cnt=0;
int m=0;
System.out.print("Enter string length: ");
int a=input.nextInt();
System.out .print("Enter a string: ");
for(int i=0;i<a;i++)
{
arr[i]=input.nextInt();
}
for(int i=0;i<a;i++)
{
if(arr[i]==1)
cnt++;
else
cnt=0;
if(cnt==5)
{
if(arr[i+1]==0)
i=i++;
m=i+1;
cnt=0;
}
}
System.out.println("Your String is:");
for(int i=0;i<a;i++)
{
if(i==m)
{}
else
System.out.print(arr[i]);
}
}
}
Program of BitStuffing
//Inserting a zero(0) after a series of five consecutive one's(1)
import java.util.*;
class BitStuffing
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int []arr=new int[20];
int cnt=0;
System.out.print("Enter string length: ");
int a=input.nextInt();
System.out .print("Enter a string: ");
for(int i=0;i<a;i++)
{
arr[i]=input.nextInt();
}
for(int i=0;i<a;i++)
{
if(arr[i]==1)
cnt++;
else
cnt=0;
if(cnt==5)
{
arr[i]=10;
cnt=0;
}
}
System.out.println("Your String is:");
for(int i=0;i<a;i++)
{
System.out.print(arr[i]);
}
}
}
Program to find out number of IP's conected to the given website
import java.net.*;
import java.util.*;
class NoOfIp
{
public static void main(String args[])throws Exception
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the url: ");
String site=s.next();
InetAddress[] i=InetAddress.getAllByName(site);
int j;
for(j=0;j<i.length;j++)
System.out.println(i[j]);
System.out.println("Number of ip found are: "+j );
}
}
Program to fetch the source code of a given website
import java.net.*;
import java.util.*;
import java.io.*;
class FetchCode
{
public static void main(String args[])throws Exception
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the url: ");
String site=s.next();
URL u=new URL(site);
BufferedReader br=new BufferedReader(new InputStreamReader(u.openStream()));
String line;
while((line=br.readLine())!=null)
System.out.println(line);
br.close();
}
}
import java.util.*;
import java.io.*;
class FetchCode
{
public static void main(String args[])throws Exception
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the url: ");
String site=s.next();
URL u=new URL(site);
BufferedReader br=new BufferedReader(new InputStreamReader(u.openStream()));
String line;
while((line=br.readLine())!=null)
System.out.println(line);
br.close();
}
}
Program to obtain the IP address of a given website address.
import java.net.*;
import java.util.*;
class Ip
{
public static void main(String args[])
{
try{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the url");
String url=sc.next();
//String url=args[0]; //By Command line
InetAddress i=InetAddress.getByName(url);
System.out.println("Host address:" +i.getHostAddress());
System.out.println("Host name :"+i.getHostName());
}
catch(Exception e)
{}
}
}
PROGRAM TO FIND OUT YOUR OWN IP ADDRESS.
class MyIp
{
public static void main(String args[])
{
try
{
InetAddress i =InetAddress.getLocalHost();
String name=i.getHostAddress();
String hname=i.getHostName();
System.out.println("Ip address= "+name);
System.out.println("Ip name= "+i);
System.out.println("Host name= "+hname);
}
catch(Exception e)
{}
}
}
No comments:
Post a Comment