1) Java Program to count the total number of characters in a string
public class Practice {
public static void main(String[] args) {
String a = "Hi This is Dani";
int count = 0;
for (int i =0; i < [Link](); i++) {
if ([Link](i) != ' ')
count++;
}
[Link](count);
}
}
Output: 12
2) Java Program to count the total number of characters in a 2
strings:
3) Java Program to count the total number of punctuation characters
exists in a String
public class punctuation {
public static void main (String [] args) {
int countPuncMarks = 0;
String str = "Good Morning! Mr. James Potter. Had your
breakfast?";
for (int i = 0; i < [Link](); i++) {
if([Link](i) == '!' || [Link](i) == ',' || [Link](i) ==
';' || [Link](i) == '.' || [Link](i) == '?' || [Link](i) == '-' ||
[Link](i) == '\'' || [Link](i) == '\"' || [Link](i) == ':') {
countPuncMarks++;
}
}
[Link]("Total number of punctuation characters : "
+ countPuncMarks);
}
}
Output:
Total number of punctuation characters : 4
4) Java Program to count the total number of vowels and consonants
in a string:
public static void main(String[] args) {
int vCount = 0, cCount = 0;
String str = "This is a really simple sentence";
str = [Link]();
for(int i = 0; i < [Link](); i++) {
if([Link](i) == 'a' || [Link](i) == 'e' || [Link](i) ==
'i' || [Link](i) == 'o' || [Link](i) == 'u') {
vCount++;
}
else if([Link](i) >= 'a' && [Link](i)<='z') {
cCount++;
}
}
[Link]("Number of vowels: " + vCount);
[Link]("Number of consonants: " + cCount);
}
}
Output:
Number of vowels: 10
Number of consonants: 17
5) Java Program to determine whether two strings are the
anagram: ?
6) Java Program to divide a string in 'N' equal parts.
import [Link].*;
import [Link];
import [Link];
public class Sample {
static boolean areAnagram(char[] str1, char[] str2)
{
int n1 = [Link];
int n2 = [Link];
if (n1 != n2)
return false;
[Link](str1);
[Link](str2);
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
public static void main(String args[])
{
char str1[] = { 'g', 'r', 'a', 'm' };
char str2[] = { 'a', 'r', 'm' };
if (areAnagram(str1, str2))
[Link]("The two strings are"
+ " anagram of each other");
else
[Link]("The two strings are not"
+ " anagram of each other");
}
}
Output:
The two strings are not anagram of each other
7) Java Program to find all subsets of a string:
public static void main(String[] args) {
String str = "FUN";
int len = [Link]();
int temp = 0;
String arr[] = new String[len*(len+1)/2];
for(int i = 0; i < len; i++) {
for(int j = i; j < len; j++) {
arr[temp] = [Link](i, j+1);
temp++;
}
}
[Link]("All subsets for given string are: ");
for(int i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
}
}
Output:
F
FU
FUN
U
UN
N
8) Java Program to find the longest repeating sequence in a string:
public static String lcp(String s, String t){
int n = [Link]([Link](),[Link]());
for(int i = 0; i < n; i++){
if([Link](i) != [Link](i)){
return [Link](0,i);
}
}
return [Link](0,n);
}
public static void main(String[] args) {
String str = "acbdfghybdf";
String lrs="";
int n = [Link]();
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
String x = lcp([Link](i,n),[Link](j,n));
if([Link]() > [Link]()) lrs=x;
}
}
[Link]("Longest repeating sequence: "+lrs);
}
}
Output:
Longest repeating sequence: bdf
9) Java Program to find all the permutations of a string:
public static void main(String[] args)
{
String str = "ABC";
int n = [Link]();
Permutation permutation = new Permutation();
[Link](str, 0, n-1);
}
private void permute(String str, int l, int r)
{
if (l == r)
[Link](str);
else
{
for (int i = l; i <= r; i++)
{
str = swap(str,l,i);
permute(str, l+1, r);
str = swap(str,l,i);
}
}
}
public String swap(String a, int i, int j)
{
char temp;
char[] charArray = [Link]();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return [Link](charArray);
}
Output:
ABC
ACB
BAC
BCA
CBA
CAB
10) Java Program to remove all the white spaces from a string
public class removeWhiteSpace {
public static void main(String[] args) {
String str1="Welcome to Java";
//Removes the white spaces using regex
str1 = [Link]("\\s+", "");
[Link]("String after removing the white spaces : "
+ str1);
}
}
Output
------
String after removing the white spaces : WelcometoJava
11) Java Program to replace lower-case characters with upper-case
and vice-versa
Public class Sample{
public static void main(String[] args) {
String a = "java";
String a1 = [Link]();
[Link](a1);
}
}
Output
------
JAVA
public class Sample{
public static void main(String[] args) {
String a = "JAVA";
String a1 = [Link]();
[Link](a1);
}
}
Output
------
java
12) Java Program to replace the spaces of a string with a specific
character
public class Replace{
public static void main(String[] args) {
String a ="Welcome to Java";
String a1 = [Link](" ","$");
[Link](a1);
}
}
13) Java Program to determine whether a given string is palindrome
public class PalindromeString{
public static void main(String[] args) {
String a = "madam";
String a2 = "";
for(int i=[Link]()-1;i>=0;i--){
char ch = [Link](i);
a2= a2+ch;
}
[Link](a2);
if([Link](a2)) {
[Link]("Palindrome String");
}else {
[Link](" Not Palindrome String");
}
}}
14) Java Program to determine whether one string is a rotation of
another
15) Java Program to find maximum and minimum occurring
character in a string
16) Java Program to find Reverse of the string
public class Employee {
public static void main(String[] args) {
String name = "Welcome";
String res = "";
for (int i = [Link]() - 1; i >= 0; i--) {
char ch = [Link](i);
res = res + ch;
}
[Link](res);
}
}
Output : emocleW
17) Java program to find the duplicate characters in a string
18) Java program to find the duplicate words in a string
19) Java Program to find the frequency of characters
------------------------------------------------------------------
public class FrequencyOfChar {
public static void main(String[] args) {
String name = "Welcome";
Map<Character, Integer> emp = new
LinkedHashMap<>();
char[] ch = [Link]();
for (char c : ch) {
if ([Link](c)) {
int count = [Link](c);
[Link](c, count + 1);
} else {
[Link](c, 1);
}
[Link](emp);
20) Java Program to find the largest and smallest word in a string
-------------------------------------------------------------------------
public class LargestAndSmallestWordInTheString {
public static void main(String[] args){
String string = "AiiTE June Batch";
String word = "", small = "", large="";
String[] words = new String[100];
int length = 0;
string = string + " ";
for(int i = 0; i < [Link](); i++){
if([Link](i) != ' '){
word = word + [Link](i);
}
else{
words[length] = word;
length++;
word = "";
}
}
small = large = words[0];
for(int k = 0; k < length; k++){
if([Link]() > words[k].length())
small = words[k];
if([Link]() < words[k].length())
large = words[k];
}
[Link]("Smallest word: " + small);
[Link]("Largest word: " + large);
}
}
21) Java Program to find the most repeated word in a text file
------------------------------------------------------------------------------
22) Java Program to find the number of the words in the given text
file
------------------------------------------------------------------------------
23) Java Program to separate the Individual Characters from a String
------------------------------------------------------------------------------
public class SeparateChar {
public static void main(String[] args)
{
String string = " JAVA SELENIUM ";
[Link](
"Individual characters from given string: ");
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i) + " ");
}
}
}
24) .
25) Java Program to print smallest and biggest possible palindrome
word in a given string
------------------------------------------------------------------------------------------------
------
public class largestAndSmallestPalindromeWordsInSentence
{
public static void main(String[] args)
{
String
sen="",wd="",wd1="",largestPalindrome="",smallestPalindrome="";
char ch=' ',ch1=' ';
int i=0,len=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence");
sen = [Link]();
sen=sen+" ";
sen=[Link]();
len=[Link]();
smallestPalindrome=sen;
for(i=0;i< len;i++)
{
ch=[Link](i);
if(ch==' ')
{
if([Link](wd1)==true)
{
if([Link]()>[Link]())
{
largestPalindrome=wd;
}
if([Link]()< [Link]())
{
smallestPalindrome=wd;
}
}
wd1="";
wd="";
}
else
{
wd=wd+ch;
wd1=ch+wd1;
}
}
[Link]("Largest Palindrome words in
sentence:"+largestPalindrome);
[Link]("Smallest Palindrome words in
sentence:"+smallestPalindrome);
}
}
26) Reverse String in Java Word by Word
------------------------------------------------------------
public class ReverseStringWordByWordProgram
{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Original string : ");
String originalStr = [Link]();
[Link]();
String words[] = [Link]("\\s");
String reversedString = "";
for (int i = 0; i < [Link]; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = [Link]() - 1; j >= 0; j--) {
reverseWord = reverseWord + [Link](j);
}
reversedString = reversedString + reverseWord + " ";
}
// Displaying the string after reverse
[Link]("Reversed string : " +
reversedString);
}
}
27) Reserve String without reverse() function.
-------------------------------------------------------
public class Employee {
public static void main(String[] args) {
String name = "Welcome";
String res = "";
for (int i = [Link]() - 1; i >= 0; i--) {
char ch = [Link](i);
res = res + ch;
}
[Link](res);
}
}