Wta Module 3
Wta Module 3
OUTPUT:
java -cp /tmp/44XeJS1nAj StringOperations
Length of first string is 6
Length of second string is 5
Is first string empty? false
Character at position 4 of first string d
Is first and second string equal? false
Is second and third string equal(ignore case)? True
Compare first and second string? 15
Compare second and third string(ignore case)? 0
Second string starts with H? False
First string ends with space? True
Concatenated string first and second is Hello World
Substring of res World
Replace of l with z Hezzo Worzd
Replace of z with l Hello World
Replace of z with l Hezlo World
HEl is in res false
Hello World
Reverse of res is dlroW olleH
Lowercase of res is hello world
Uppercase of res is HELLO WORLD
String is Java Programming
Trimmed string is Java Programming
2. Write a java program that displays the number of characters, lines and words in
a given file
import java.util.*;
import java.io.*;
}
}
OUTPUT:
[viics134@localhost ~]$ vi count.txt
[viics134@localhost ~]$ java CountWlc count.txt
Number of Characters: 35
Number of Words: 9
Number of Lines: 4
3. Write a Java program that reads on file name from the user, then displays
information about whether the file exists, whether the file is readable, whether the
file is writable, Absolute path of file and the length of the file in bytes?
import java.io.File;
class FileDemo {
public static void main(String args[ ]) {
File f1 = new File("count.txt");
if(f1.exists())
{
System.out.println("File Name: " + f1.getName());
System.out.println("Path: " + f1.getPath());
System.out.println("Abs Path: " + f1.getAbsolutePath());
System.out.println("writeable" +f1.canWrite());
System.out.println("readable" +f1.canRead());
System.out.println("File size: " + f1.length() + " Bytes");
}
else{
System.out.println("The file does not exist.");
}
}
}
OUTPUT:
[viics134@localhost ~]$ java FileDemo
File Name: count.txt
Path: count.txt
Abs Path: /home/viics134/count.txt
Writeable true
readabletrue
File size: 39 Bytes
4. Write a Java program that reads a file and displays the file on the screen, with a
line number before each line?
import java.io.*;
class LineNum{
public static void main(String[] args){
String line;
for(int i=0;i<args.length;i++){
try {
LineNumberReader br= new LineNumberReader
(newFileReader(args[i]));
while((line=br.readLine())!=null) {
System.out.println(br.getLineNumber()+". "+line);
}
}catch(IOException e) {
System.out.println("Error "+e);
}
}
}
}
OUTPUT:
5. Write a Java program that displays the number of characters, lines and words
in a text file.
Same as 2ques.
6. Write a Java program for sorting a given list of names in ascending order.
import java.util.Scanner;
public class SortStrings {
public static void main(String[] args) {
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to sort:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i=0;i<n;i++)
names[i] = s1.nextLine();
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
if (names[i].compareTo(names[j])>0) {
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i=0;i<n-1;i++)
System.out.print(names[i] + ",");
System.out.println(names[n - 1]);
s.close();
s1.close();
}
}
OUTPUT:
Enter number of names you want to sort:3
Enter all the names:
sanjana
isha
xerox
Names in Sorted Order:isha,sanjana,xerox
7. Write a Java program that checks whether a given string is a palindrome or not.
Ex: MADAM is a palindrome.
import java.util.*;
class PalindromeExample
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string ");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("String is a palindrome.");
else
System.out.println("String isn't a palindrome.");
}
}
OUTPUT:
java -cp /tmp/44XeJS1nAj PalindromeExample
Enter a string madam
String is a palindrome.
8. Write a program in java to efficiently read data from the keyboard and write the
same data into the file.
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
}
}
OUTPUT:
9. Write a program in java to read filename from user, read data from file using
File Reader and improve its efficiency and display the contents of the file.
import java.util.*;
import java.io.*;
public class readFile{
public static void main(String[]a){
try{
Scanner s = new Scanner(System.in);
System.out.println("Enter the filename: ");
String x = s.nextLine();
BufferedReader br = new BufferedReader(new FileReader(x));
Scanner sr = new Scanner(br);
System.out.println("File contents....");
while(sr.hasNextLine()){
String data = sr.nextLine();
System.out.println(data);
}
}catch(Exception e){
System.out.println(e);}
}
}
OUTPUT:
Enter the filename:
sanjana.txt
File contents....
Hi Sanjana Urs, Welcome to Engineering
10. Write a program that implements the client/server application. The client
sends the data to the server; the server receives the data, uses it to produce a
result and then sends the result back to the client. The client displays the result
on the console.
File: MyServer.java
import java.net.*;
import java.io.*;
class MyServer{
Socket s=ss.accept();
String str="",str2="";
while(!str.equals("stop")){
str=din.readUTF();
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
din.close();
s.close();
ss.close();
}}
File: MyClient.java
import java.net.*;
import java.io.*;
class MyClient{
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
dout.close();
s.close();
}}