import java.util.Scanner;
public class WordFrequencyCounter {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("enter main string");
String s = scan.nextLine();
System.out.println("enter string to be searched");
String f = scan.nextLine();
s = s + " ";
int l = s.length();
char a;
int c = 0;
String s1 = "";
for (int i = 0; i < l; i++) {
a = s.charAt(i);
if (a != ' ') {
s1 = s1 + a;
} else {
if (s1.equalsIgnoreCase(f) == true) {
c++;
}
s1 = "";
}
}
System.out.println("Frequency of the word " + f + " is " + c);
}
}
Output:
Enter main string
Umesh Kumar Kushwaha Kumar
enter string to be searched
Kumar
Frequency of the word Kumar is 2
public class WordFrequencyCounter {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("enter main string");
String s = scan.nextLine();
System.out.println("enter string to be searched");
String f = scan.nextLine();
s = s + " ";
int l = s.length();
char a;
int c = 0;
String s1 = "";
for (int i = 0; i < l; i++) {
a = s.charAt(i);
if (a != ' ') {
s1 = s1 + a;
} else {
if (s1.equalsIgnoreCase(f) == true) {
c++;
}
s1 = "";
}
}
System.out.println("Frequency of the word " + f + " is " + c);
}
}
Output:
Enter main string
Umesh Kumar Kushwaha Kumar
enter string to be searched
Kumar
Frequency of the word Kumar is 2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveVowels
{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1, str2 = "";
char ch, ch1;
int i, len;
System.out.println("Enter the Sentence:");
str1 = br.readLine();
len = str1.length();
for (i = 0; i < len; i++) {
ch = str1.charAt(i);
ch1 = Character.toLowerCase(ch);
switch (ch1) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break;
default:
str2 = str2 + ch;
} //switch ends
} //for ends
System.out.println("Modified string without vowels:" + str2);
} //method ends
}
Output:
Enter the Sentence:
umesh kumar kushwaha
Modified string without vowels:msh kmr kshwh
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveVowels
{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1, str2 = "";
char ch, ch1;
int i, len;
System.out.println("Enter the Sentence:");
str1 = br.readLine();
len = str1.length();
for (i = 0; i < len; i++) {
ch = str1.charAt(i);
ch1 = Character.toLowerCase(ch);
switch (ch1) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break;
default:
str2 = str2 + ch;
} //switch ends
} //for ends
System.out.println("Modified string without vowels:" + str2);
} //method ends
}
Output:
Enter the Sentence:
umesh kumar kushwaha
Modified string without vowels:msh kmr kshwh
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveCommonDuplicateChar
{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1, s2, s3 = "", s4 = "";
int a[], b[], x = 0, y = 0;
int len1, len2;
char ch;
int i, j;
System.out.println("Enter the first sentence:");
s1 = br.readLine().trim();
System.out.println("Enter the second sentence:");
s2 = br.readLine().trim();
len1 = s1.length();
len2 = s2.length();
a = new int[len1];
b = new int[len2];
for (i = 0; i < len1; i++) {
ch = s1.charAt(i);
for (j = 0; j < len2; j++) {
if (ch == s2.charAt(j)) {
break;
}
}
if (j != len2) {
a[x++] = i;
b[y++] = j;
}
}
for (i = 0; i < len1; i++) {
for (j = 0; j < x; j++) {
if (i == a[j]) {
break;
}
}
if (j == x) {
s3 = s3 + s1.charAt(i);
}
}
for (i = 0; i < len2; i++) {
for (j = 0; j < y; j++) {
if (i == b[j]) {
break;
}
}
if (j == y) {
s4 = s4 + s2.charAt(i);
}
}
System.out.println("Original string1=" + s1 + " Modified string1=" + s3);
System.out.println("Original string2=" + s2 + " Modified string2=" + s4);
}
}
Output:
Enter the first sentence:
Umesh Kumar Kushwaha
Enter the second sentence:
Rahul Kumar Singh
Original string1=Umesh Kumar Kushwaha Modified string1=Uessw
Original string2=Rahul Kumar Singh Modified string2=Rlua Singh
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveCommonDuplicateChar
{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1, s2, s3 = "", s4 = "";
int a[], b[], x = 0, y = 0;
int len1, len2;
char ch;
int i, j;
System.out.println("Enter the first sentence:");
s1 = br.readLine().trim();
System.out.println("Enter the second sentence:");
s2 = br.readLine().trim();
len1 = s1.length();
len2 = s2.length();
a = new int[len1];
b = new int[len2];
for (i = 0; i < len1; i++) {
ch = s1.charAt(i);
for (j = 0; j < len2; j++) {
if (ch == s2.charAt(j)) {
break;
}
}
if (j != len2) {
a[x++] = i;
b[y++] = j;
}
}
for (i = 0; i < len1; i++) {
for (j = 0; j < x; j++) {
if (i == a[j]) {
break;
}
}
if (j == x) {
s3 = s3 + s1.charAt(i);
}
}
for (i = 0; i < len2; i++) {
for (j = 0; j < y; j++) {
if (i == b[j]) {
break;
}
}
if (j == y) {
s4 = s4 + s2.charAt(i);
}
}
System.out.println("Original string1=" + s1 + " Modified string1=" + s3);
System.out.println("Original string2=" + s2 + " Modified string2=" + s4);
}
}
Output:
Enter the first sentence:
Umesh Kumar Kushwaha
Enter the second sentence:
Rahul Kumar Singh
Original string1=Umesh Kumar Kushwaha Modified string1=Uessw
Original string2=Rahul Kumar Singh Modified string2=Rlua Singh
import java.io.BufferedReader;
public class ReverseOfString {
public static String reverseString(String s)
{
int l = s.length();
String backward = "";
for (int i = l - 1; i >= 0; i--)
{
backward = backward + s.charAt(i);
}
return backward;
}
public static void main(String args[]) {
String backwards = reverseString("Umesh Kushwaha");
System.out.println("Reverse String is " + backwards);
}
}
Output:
Reverse String is ahawhsuK hsemU
public class ReverseOfString {
public static String reverseString(String s)
{
int l = s.length();
String backward = "";
for (int i = l - 1; i >= 0; i--)
{
backward = backward + s.charAt(i);
}
return backward;
}
public static void main(String args[]) {
String backwards = reverseString("Umesh Kushwaha");
System.out.println("Reverse String is " + backwards);
}
}
Output:
Reverse String is ahawhsuK hsemU
import java.io.BufferedReader;
class FindMinMaxString {
public static void main(String args[]) {
findMethod("My name is Umesh Kushwaha");
}
static public void findMethod(String s) {
String str = s + " ";
char ch = ' ';
int len = str.length(), l = 0;
int min = len, max = 0;
String shortest_word = "", longest_word = "", word = "";
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (ch != ' ') {
word += ch;
} //if ends
else {
l = word.length();
if (l < min) {
min = l;
shortest_word = word;
} //if ends
if (l > max) {
max = l;
longest_word = word;
}
word = "";
}
}
System.out.println("Shortest word = " + shortest_word + " with length " + min);
System.out.println("Longest word = " + longest_word + " with length " + max);
}
}
Output:
Shortest word = My with length 2
Longest word = Kushwaha with length 8
class FindMinMaxString {
public static void main(String args[]) {
findMethod("My name is Umesh Kushwaha");
}
static public void findMethod(String s) {
String str = s + " ";
char ch = ' ';
int len = str.length(), l = 0;
int min = len, max = 0;
String shortest_word = "", longest_word = "", word = "";
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (ch != ' ') {
word += ch;
} //if ends
else {
l = word.length();
if (l < min) {
min = l;
shortest_word = word;
} //if ends
if (l > max) {
max = l;
longest_word = word;
}
word = "";
}
}
System.out.println("Shortest word = " + shortest_word + " with length " + min);
System.out.println("Longest word = " + longest_word + " with length " + max);
}
}
Output:
Shortest word = My with length 2
Longest word = Kushwaha with length 8
Example.
Enter your name
Umesh Kumar Kushwaha
Initials are U.K.K.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class InitialName {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name");
String s = br.readLine(), s1 = "";
s = " " + s;
int len = s.length();
char a;
for (int i = 0; i < len; i++) {
a = s.charAt(i);
if (a == ' ') {
s1 = s1 + s.charAt(i + 1) + ".";
}
}
System.out.println("Initials are " + s1);
}
}
Output:
Enter your name
Umesh Kumar Kushwaha
Initials are U.K.K.
Enter your name
Umesh Kumar Kushwaha
Initials are U.K.K.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class InitialName {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name");
String s = br.readLine(), s1 = "";
s = " " + s;
int len = s.length();
char a;
for (int i = 0; i < len; i++) {
a = s.charAt(i);
if (a == ' ') {
s1 = s1 + s.charAt(i + 1) + ".";
}
}
System.out.println("Initials are " + s1);
}
}
Output:
Enter your name
Umesh Kumar Kushwaha
Initials are U.K.K.
class CalculateAscii {
public static void main(String args[]) {
word("Umesh Kushwaha");
}
static void word(String s) {
int len = s.length();
int a;
char a1;
for (int i = 0; i < len; i++) {
a1 = s.charAt(i);
a = a1;
System.out.println(a1 + "–>" + a);
}
}
}
Output:
U–>85
m–>109
e–>101
s–>115
h–>104
–>32
K–>75
u–>117
s–>115
h–>104
w–>119
a–>97
h–>104
a–>97
public static void main(String args[]) {
word("Umesh Kushwaha");
}
static void word(String s) {
int len = s.length();
int a;
char a1;
for (int i = 0; i < len; i++) {
a1 = s.charAt(i);
a = a1;
System.out.println(a1 + "–>" + a);
}
}
}
Output:
U–>85
m–>109
e–>101
s–>115
h–>104
–>32
K–>75
u–>117
s–>115
h–>104
w–>119
a–>97
h–>104
a–>97
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
class CountFrequencyOfWord {
public static void main(String args[]) {
try {
input();
} catch (IOException ex) {
Logger.getLogger(CountFrequencyOfWord .class.getName()).log(Level.SEVERE, null, ex);
}
}
static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence");
String s = br.readLine();
System.out.println("Enter the letter ");
char a = (char) br.read();
int len = s.length();
int c = 0;
char x;
for (int i = 0; i < len; i++) {
x = Character.toLowerCase(s.charAt(i));
if (x == a)
c = c + 1;
}
System.out.println("Frequency of the letter " + a + " in the sentence " + s + " is " + c);
}
}
Output:
Enter a sentence
Hi my name is umesh
Enter the letter
m
Frequency of the letter m in the sentence "Hi my name is umesh" is 3
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
class CountFrequencyOfWord {
public static void main(String args[]) {
try {
input();
} catch (IOException ex) {
Logger.getLogger(CountFrequencyOfWord .class.getName()).log(Level.SEVERE, null, ex);
}
}
static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence");
String s = br.readLine();
System.out.println("Enter the letter ");
char a = (char) br.read();
int len = s.length();
int c = 0;
char x;
for (int i = 0; i < len; i++) {
x = Character.toLowerCase(s.charAt(i));
if (x == a)
c = c + 1;
}
System.out.println("Frequency of the letter " + a + " in the sentence " + s + " is " + c);
}
}
Output:
Enter a sentence
Hi my name is umesh
Enter the letter
m
Frequency of the letter m in the sentence "Hi my name is umesh" is 3
class RemoveDuplicate {
public static void main(String args[]) {
remove("123aa bbbcc c2589 99oppq rtyyy");
}
static void remove(String s) {
int l = s.length();
int c;
String org = s, s1 = "";
for (int i = 0; i < (l - 1); i++) {
s1 = s.substring(0, i + 1);
c = 0;
for (int j = i + 1; j < l; j++) {
if (s.charAt(i) == s.charAt(j)) {
c++;
continue;
} else
s1 = s1 + s.charAt(j);
}
s = s1;
s1 = "";
if (c > 0)
l -= c;
}
System.out.println("Original String:" + org);
System.out.println("String after removing duplicates: " + s);
}
}
Output:
Original String:123aa bbbcc c2589 99oppq rtyyy
String after removing duplicates: 123a bc589opqrty
public static void main(String args[]) {
remove("123aa bbbcc c2589 99oppq rtyyy");
}
static void remove(String s) {
int l = s.length();
int c;
String org = s, s1 = "";
for (int i = 0; i < (l - 1); i++) {
s1 = s.substring(0, i + 1);
c = 0;
for (int j = i + 1; j < l; j++) {
if (s.charAt(i) == s.charAt(j)) {
c++;
continue;
} else
s1 = s1 + s.charAt(j);
}
s = s1;
s1 = "";
if (c > 0)
l -= c;
}
System.out.println("Original String:" + org);
System.out.println("String after removing duplicates: " + s);
}
}
Output:
Original String:123aa bbbcc c2589 99oppq rtyyy
String after removing duplicates: 123a bc589opqrty

