-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9876ca1
commit 81a4921
Showing
30 changed files
with
1,002 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+" "); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+" "); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+" "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.