These codes are implemented and provided by 'Shriyam Verma' from The J.P. University, Noida.
import java.util.*;
public class StringPalindrome
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String s=sc.nextLine(); //can contain spaces
int len=s.length(); //length of String s
  
String rev=""; //to store reverse of s
  
for(int i=0;i<len;i++)
{
//Concat each character one by one to the starting of rev
// And change the value of rev
rev=s.charAt(i) + rev;
}
  
//Check if entered String 's' is Exactly Same as that of its Reverse 'rev'
if(s.equals(rev))
System.out.println(s+" is Palindrome");
else
System.out.println(s+" is not Palindrome");
}
}
import java.util.*;
public class LengthOfString
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String s=sc.nextLine(); //String containing spaces allowed
// concatinate a \0 character after string, because in java strings are never terminated
// by \0 char (in C and C++ strings are terminated by \0 char)
s = s + "\0";
             
// this variable will store the length of string
int i = 0;
// get a single char from string till \0 char is not found
while(s.charAt(i) != '\0')
{
// increment the value of 'i'
i++;
}
System.out.println("length of string is "+i);
}
  
   
   
  
  
   
  
  
   
   
  
  
  
  
  
   
   
    
    
    
     
  
  
  
    
    
    
    
    
    
    
     
     
       
        
     
  
  
  
   
   
   
   
   
   
   
   
   
   
   
   
******************************************************
note: save this class inside StringPalindrome .java file
import java.util.*;
public class StringPalindrome
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String s=sc.nextLine(); //can contain spaces
int len=s.length(); //length of String s
String rev=""; //to store reverse of s
for(int i=0;i<len;i++)
{
//Concat each character one by one to the starting of rev
// And change the value of rev
rev=s.charAt(i) + rev;
}
//Check if entered String 's' is Exactly Same as that of its Reverse 'rev'
if(s.equals(rev))
System.out.println(s+" is Palindrome");
else
System.out.println(s+" is not Palindrome");
}
}
******************************************************
note: save this class inside LengthOfString.java file
public class LengthOfString
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String s=sc.nextLine(); //String containing spaces allowed
// concatinate a \0 character after string, because in java strings are never terminated
// by \0 char (in C and C++ strings are terminated by \0 char)
s = s + "\0";
// this variable will store the length of string
int i = 0;
// get a single char from string till \0 char is not found
while(s.charAt(i) != '\0')
{
// increment the value of 'i'
i++;
}
System.out.println("length of string is "+i);
}
******************************************************
note: save this class inside ConvertDecimal.java file
import java.util.Scanner;
public class ConvertDecimal 
{
 //Static Class Methods
 static void toBinary(int num)
 {
  String s=""; //to store Binary Number
  int temp=num; //create a copy of original number 
  while(temp!=0) 
  {
   int d=temp%2; //calculate remainder 0 or 1
   s = d + s; //concat 0/1 at the beginning of 's' and put into 's'
   //if we concatinated at the end of 's' then we need to reverse it
   temp/=2; //Divide temp by 2 and store quotient in temp
  }
  //Calculations are wrt 2
  System.out.println("Binary of "+num+": "+s);
 }
 static void toOctal(int num)
 {
  String s=""; //to store Octal Number
  int temp=num; //create a copy of original number
  while(temp!=0)
  {
   int d=temp%8; //calculate remainder 0 to 7
   s = d + s;  //Concat remainder at the beginning of 's'
   temp/=8;
  }
  //Same as Binary except that calculations are wrt 8
  System.out.println("Octal of "+num+": "+s);
 }
 static void toHexaDecimal(int num)
 {
  String s=""; //to store HexaDecimal Number
  int temp=num; //create a copy of original number
  while(temp!=0)
  {
   int d=temp%16; //calculate remainder 0 to 15
   if(d<=9)  //Concat 0-9
    s = d + s;
   else   //Concat A-F
    s = (char)(55+d) + s;
   temp/=16;
  }
  //Calculations are wrt 16
  System.out.println("HexaDecimal of "+num+": "+s);
 }
 public static void main(String[] args) 
 {
  Scanner sc=new Scanner(System.in);
  System.out.print("Enter a number: ");
  int n=sc.nextInt();  //Decimal Number
  System.out.println("#--------------------Menu--------------------#");
  System.out.println("1- Convert Into Binary");
  System.out.println("2- Convert Into Octal");
  System.out.println("3- Convert Into HexaDecimal");
  System.out.println("#--------------------------------------------#");
  System.out.print("Enter Choice: ");
  int ch=sc.nextInt();
  if(ch==1)
   toBinary(n);
  else if(ch==2)
   toOctal(n);
  else if(ch==3)
   toHexaDecimal(n);
  else
   System.out.println("Invalid Choice.");
  sc.close();
 }
}
******************************************************
note: save this class inside Authentication .java file
import java.util.*;
public class Authentication 
{
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  System.out.print("Enter Username: ");
  String user=sc.nextLine();
  System.out.print("Enter Password: ");
  String pass=sc.nextLine();
  //Check for Username and Password Authentication.
  if(user.equalsIgnoreCase("Admin#1") && pass.equals("AdminTvJ"))
  {
   System.out.println("Welcome..!");
   System.out.println("Enter a paragraph:");
   String para=sc.nextLine(); //paragraph of multiple sentences
   System.out.print("Enter a word: ");
   String word=sc.nextLine(); //word to be searched in para
   //Case Sensitive search for occurence
   int fi=para.indexOf(word); //first occurence of word in para 
   int li=para.lastIndexOf(word); //last occurence of word in para
   //Check if the word is present in para or not
   if(fi!=-1)
   {
    System.out.println("First index: "+fi);
    System.out.println("Last index: "+li);
    int index=fi; //to store next occurence of word
    int freq=0;  //to calculate frequency of word in para
    System.out.print("All indexes:");
    while(index!=-1) //word found
    {
     freq++;
     System.out.print(" "+index); //print index
     //Check next occurence of word after prev index
     index=para.indexOf(word,index+1); 
    }
    System.out.println("\nFrequency: "+freq);
   }
   else
   {
    System.out.println("No such word exists in the Paragraph..!!");
   }
  }
  else 
  {
   System.out.println("Invalid Credentials..!");
  }
 }
}
******************************************************
note: save this class inside TheFISAssignment.java file
import java.io.FileInputStream;
import java.util.Scanner;
public class TheFISAssignment
{
 public static void main(String[] args) 
 {
  Scanner sc =new Scanner(System.in);
  System.out.print("Please enter a path: ");
  String path=sc.nextLine();
  //convert into LowerCase so it's easy to compare extension of file 
  // (as endsWith is Case Sensitive)
  path=path.toLowerCase(); 
  if(path.endsWith(".txt") || path.endsWith(".java") || path.endsWith(".html"))
  {
   try
   {
    //Open a file in read mode
    FileInputStream fis=new FileInputStream(path);
    //no. of Bytes that can be read from fis(path) 
    int size=fis.available(); 
    //Create byte array to store data of file
    byte[] data=new byte[size];
    //Copy content from file to byte array 'data' in 1 go
    fis.read(data);  
    //Converting byte array 'data' to String using its Constructor
    String s=new String(data);
    System.out.println("\nData in File:");
    System.out.println(s);
    System.out.print("Press 1 to continue: ");
    char ch=(sc.nextLine()).charAt(0);
    if(ch=='1')
    {
     int alpha,lc,uc,n,lines,words,spaces,i;
     alpha=lc=uc=n=words=spaces=0;
     //Atleast 1 line is always there
     lines=1; 
     //Iterate over String s
     for(i=0;i<s.length();i++) 
     {
      //Check if Alphabet
      if(Character.isLetter(s.charAt(i)))  
      {
       //Check LowerCase Alphabet from 
                                                        //ASCII 97 to 122
       if(s.charAt(i)>=97)  
       {
        lc++;
       }
       //else UpperCase Alphabet from
                                                        // ASCII 65 to 90
       else  
       {
        uc++;
       }
       //count no. of alphabets.. 
       alpha++; 
       // if ith char is alphabet and next char is not an alphabet
       // then word completed so count it..
       // Here we are not considering the last index
       // to prevent Runtime error due to charAt(i+1)
       if(i+1!=s.length() && !Character.isLetter
       (s.charAt(i+1)))
       {
        //Word is a combination of consequent alphabets
        words++; 
       }
      }
      //Check if number: from ASCII 48 to 57
      else if(Character.isDigit(s.charAt(i)))  
      {
       n++;
      }
      //Check if space ' '
      else if(s.charAt(i)==32) 
      {
       spaces++;
      }
      //Check if NewLine '\n'
      else if(s.charAt(i)==10) 
      {
       lines++;
      }
     }
     //check the last char of string..
     //if it is letter then we have not counted the last 
                                        // occurring word
     if(Character.isLetter(s.charAt(i-1)))
      words++;
     System.out.println("No. of Alphabets: "+alpha);
     System.out.println("No. of UpperCase Alphabets: "+uc);
     System.out.println("No. of LowerCase Alphabets: "+lc);
     System.out.println("No. of Digits: "+n);
     System.out.println("No. of Words: "+words);
     System.out.println("No. of Lines: "+lines);
     System.out.println("No. of Spaces: "+spaces);
     System.out.println("File Size: "+size+" Bytes");
    }
   }
   catch(Exception e)
   {
    System.out.println("Error: "+e);
   }
  }
  else
  {
   System.out.println("Invalid Format..!");
  }
 }
}
******************************************************
note: save this class inside TheFOSAssignment.java file
import java.io.*;
import java.util.*;
public class FOSAssignment
{
 public static void main(String[] args) 
 {
  Scanner sc =new Scanner(System.in);
  System.out.print("Please enter source path: ");
  String src=sc.next();
  System.out.print("Please enter destination path: ");
  String dst=sc.next();
  System.out.println("#--------------------MENU--------------------#");
  System.out.println("1 - Copy source to destination in Write Mode");
  System.out.println("2 - Copy source to destination in Append Mode");
  System.out.println("#--------------------------------------------#");
  System.out.print("Enter Choice: ");
  int ch=sc.nextInt();
  //this variable will be used to denote 
  // mode of opening the destination file
  boolean var=false; 
  if(ch==1)
  {
   var=false; //Write Mode
  }
  else if(ch==2)
  {
   var=true; //Append Mode
  }
  else
  {
   System.out.println("Invalid Choice..!!");
   //to exit from the program execution
   System.exit(0);  
  }
  try
  {
   //Open a file in Write or Append Mode
   FileOutputStream fos=new FileOutputStream(dst,var);
   //Open a file in Read mode
   FileInputStream fis=new FileInputStream(src);
   //no. of Bytes that can be read from source file 
   int size=fis.available(); 
   //Create byte array to store data of file
   byte[] data1=new byte[size]; 
   //Copy complete content from source file to byte array 'data1'
   fis.read(data1);
   //Copy source data from to destination file in one go   
   fos.write(data1); 
   //closing files
   fis.close();
   fos.close();
   //Opening destination file in read mode
   fis=new FileInputStream(dst);
   //no. of Bytes that can be read from destination file
   size=fis.available(); 
   //Create byte array to store data of file
   byte[] data2=new byte[size]; 
   //Copy complete content from destination file to byte array 'data2'
   fis.read(data2);
   //Converting byte array 'data2' to String using its Constructor
   String s=new String(data2); 
   System.out.println("\nData in destination file :");
   System.out.println(s);
   System.out.println("Size of destination file: "+size+"Byte");
  }
  catch(Exception ex)
  {
   System.out.println("Error: "+ex);
  }
 }
}
 
well coded.. thanks
ReplyDelete