Skip to content

Commit

Permalink
assignment 3 done except a3q2.java
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyMiniac committed Oct 19, 2020
1 parent 9876ca1 commit 81a4921
Show file tree
Hide file tree
Showing 30 changed files with 1,002 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Semester 2 (DSA)/assignment 2/demo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//a2q1.java
import java.util.*;
public class demo
public class Demo
{
public static void main(String args[])
{
Expand Down
51 changes: 51 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//A Simple Text Formatter as in text book.
import java.io.*;
import java.util.*;

public class a3q1
{
public static void main(String args[])throws IOException
{
Scanner in = new Scanner(System.in);
System.out.println("Enter The File Path : ");
String path=in.nextLine();
in.close();

FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String s="";
StringBuffer sb1 = new StringBuffer();
int len=0;

while((s=br.readLine())!=null)
{
len=len<s.length()?s.length():len;
sb1.append(s);
sb1.append((char)0);
}

String delim=""+(char)0;
s=sb1.toString();
StringTokenizer st = new StringTokenizer(s,delim);
StringBuffer sb2 = new StringBuffer();

while(st.hasMoreElements())
{
StringBuffer tmp = new StringBuffer(st.nextElement().toString());
int tmplen=tmp.length();
for(int i=tmplen; i<len; i++)
{
tmp.insert(0, " ");
}
sb2.append(tmp);
sb2.append("\n");
}

fr.close();
br.close();

FileWriter fw = new FileWriter(path);
fw.write(sb2.toString());
fw.close();
}
}
41 changes: 41 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*The global replace function is a string-processing algorithm found in every word
processor. Write a method that takes three String arguments: a document,
a target string, and a replacement string. The method should replace every
occurrence of the target string in the document with the re- placement string.
For example, if the document is “ To be or not to be, that is the question,” and
the target string is “be”, and the replacement string is “see,” the result should be,
“To see or not to see, that is the question."*/

import java.util.*;

public class a3q10
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String str=in.nextLine();
System.out.print("Enter the targetted word : ");
String tar=in.nextLine();
System.out.print("Enter the replacement word : ");
String rep=in.nextLine();
in.close();
replace(str, tar,rep);
}
public static void replace(String str, String tar, String rep)
{
StringTokenizer st = new StringTokenizer(str," ");
while(st.hasMoreElements())
{
String tmp=(String) st.nextElement();
if(tmp.equalsIgnoreCase(tar))
{
System.out.print(rep+" ");
}
else
{
System.out.print(tmp+" ");
}
}
}
}
40 changes: 40 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q11.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*Design and write a Java program that searches for single- digit numbers in a text
and changes them to their corresponding words. For ex- ample, the string
“4 score and 7 years ago” would be converted into "four score and seven years ago”.*/

import java.util.*;

public class a3q11
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a Stirng : ");
String inp=in.nextLine();
in.close();
String numbers[]= {"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",};
StringTokenizer st = new StringTokenizer(inp," ");
while(st.hasMoreElements())
{
String tmp=st.nextToken();
try
{
int i=Integer.parseInt(tmp);
System.out.print(numbers[i]+" ");
}
catch(NumberFormatException e)
{
System.out.print(tmp+" ");
}
}
}
}
31 changes: 31 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q12.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*Write a method that converts its String parameter so that letters are written in
blocks five characters long. For example, consider the following two versions of
the same sentence: Plain : This i s how we would o r d in a rily write a sentence.
Blocked : This i showw ewoul dordi n a r il ywrit easen tence.*/

import java.util.*;

public class a3q12
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String inp=in.nextLine();
in.close();
a3q12 ob = new a3q12();
ob.block(inp);
}
private void block(String inp)
{
inp=inp.replaceAll(" ", "");
for(int i=0; i<inp.length(); i++)
{
if(i%5==0 && i!=0)
{
System.out.print(" ");
}
System.out.print(inp.charAt(i));
}
}
}
42 changes: 42 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q13.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*Write a Java program to find longest Palindromic Substring within a string
Example: The given string is: thequickbrownfoxxofnworbquickthe
The longest palindrome substring in the given string is;
brownfoxxofnworb The length of the palindromic substring is: 16*/

import java.util.*;

public class a3q13
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String s=in.nextLine();
String pal="";
in.close();

for(int i=0; i<s.length(); i++)
{
for(int j=i+2; j<s.length(); j++)
{
String tmp=s.substring(i,j);
a3q13 ob = new a3q13();
if(ob.checkPalindrome(tmp))
pal=tmp.length()>pal.length()?tmp:pal;
}
}

System.out.println("Largest Palidromic String is : "+pal);
}
boolean checkPalindrome(String s)
{
for(int i=0; i<s.length()/2; i++)
{
if(s.charAt(i)!=s.charAt((s.length()-1)-i))
{
return false;
}
}
return true;
}
}
28 changes: 28 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q14.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*Write a Java program to find first non repeating character in a string .
Example: The given string is: gibblegabbler
The first non repeated character in String is: i*/

import java.util.*;

public class a3q14
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String inp=in.nextLine();
inp+=" ";
in.close();

for(int i=0; i<inp.length(); i++)
{
char ch=inp.charAt(i);
if(inp.split(ch+"").length==2)
{
System.out.println(ch+" is First Non repeated Character in the string");
System.exit(0);
}
}
System.err.println("There doesn't Exist any character that is not repeated in the string");
}
}
26 changes: 26 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*Write a java program to remove the title from a name.
Ex. Input: Mr. Sachin Tendulkar Ms. Saina Nehwal Output: Sachin Tendulkar Saina Nehwal
*/

import java.util.*;

public class a3q15
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String s=in.nextLine();
in.close();
StringTokenizer st = new StringTokenizer(s," ");
while(st.hasMoreElements())
{
String tmp=st.nextToken();
if(tmp.equalsIgnoreCase("Mr."))
{
continue;
}
System.out.print(tmp+" ");
}
}
}
25 changes: 25 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Write a java program to read two string as user input and check if first contains second?

import java.util.*;

public class a3q16
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the First String : ");
String s1=in.nextLine();
System.out.print("Enter the String String : ");
String s2=in.nextLine();
in.close();

if(s1.contains(s2))
{
System.out.println("Yes, 1st string contains the second");
}
else
{
System.out.println("No, 1st string doesn't contains the second");
}
}
}
20 changes: 20 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q17.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//Write a java code to remove all occurrences of a given character from an input String

import java.util.*;

public class a3q17
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String inp=in.nextLine();
System.out.print("Enter a character to be removed : ");
char ch=in.nextLine().charAt(0);
in.close();

String out=inp.replaceAll(ch+"", "");

System.out.println("Output : "+out);
}
}
36 changes: 36 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q18.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//Write a java code to find the longest substring without repeating characters in the given string.

import java.util.*;

public class a3q18
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String inp=in.nextLine();
in.close();
String biggest="";

for(int i=0; i<inp.length(); i++)
{
for(int j=i+1; j<=inp.length(); j++)
{
biggest=repeat(inp.substring(i,j))?inp.substring(i,j).length()>biggest.length()?inp.substring(i,j):biggest:biggest;
}
}
System.out.println("Output : "+biggest);
}
private static boolean repeat(String inp)
{
inp+=" ";
for(int i=0; i<inp.length(); i++)
{
if(inp.split(inp.charAt(i)+"").length>2)
{
return false;
}
}
return true;
}
}
24 changes: 24 additions & 0 deletions Semester 3 (CWS-1)/Assignments/Assignment 3/a3q19.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Write a java program to check if a String contains only digits.

import java.util.*;

public class a3q19
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a String : ");
String inp=in.nextLine();
in.close();

for(int i=0; i<inp.length(); i++)
{
if(Character.isDigit(inp.charAt(i)))
{
System.out.println("The Given String Contains a Number");
System.exit(0);
}
}
System.out.println("The Given String doesn't contain any number");
}
}
Loading

0 comments on commit 81a4921

Please sign in to comment.