0% found this document useful (0 votes)
3 views

String Prog

The document contains multiple Java programs that demonstrate various string manipulation techniques. These include counting characters, vowels, and consonants, checking for anagrams, finding subsets and permutations, identifying palindromes, and removing whitespace. Each program is accompanied by its output, showcasing the functionality of the code.

Uploaded by

Suresh Mano
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

String Prog

The document contains multiple Java programs that demonstrate various string manipulation techniques. These include counting characters, vowels, and consonants, checking for anagrams, finding subsets and permutations, identifying palindromes, and removing whitespace. Each program is accompanied by its output, showcasing the functionality of the code.

Uploaded by

Suresh Mano
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

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 < a.length(); i++) {
if (a.charAt(i) != ' ')
count++;
}
System.out.println(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 < str.length(); i++) {
if(str.charAt(i) == '!' || str.charAt(i) == ',' || str.charAt(i) ==
';' || str.charAt(i) == '.' || str.charAt(i) == '?' || str.charAt(i) == '-' ||
str.charAt(i) == '\'' || str.charAt(i) == '\"' || str.charAt(i) == ':') {
countPuncMarks++;
}
}
System.out.println("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 = str.toLowerCase();

for(int i = 0; i < str.length(); i++) {


if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) ==
'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
vCount++;
}
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("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 java.io.*;
import java.util.Arrays;
import java.util.Collections;

public class Sample {

static boolean areAnagram(char[] str1, char[] str2)


{
int n1 = str1.length;
int n2 = str2.length;

if (n1 != n2)
return false;

Arrays.sort(str1);
Arrays.sort(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))
System.out.println("The two strings are"
+ " anagram of each other");
else
System.out.println("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 = str.length();
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] = str.substring(i, j+1);
temp++;
}
}
System.out.println("All subsets for given string are: ");
for(int i = 0; i < arr.length; i++) {
System.out.println(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 = Math.min(s.length(),t.length());
for(int i = 0; i < n; i++){
if(s.charAt(i) != t.charAt(i)){
return s.substring(0,i);
}
}
return s.substring(0,n);
}
public static void main(String[] args) {
String str = "acbdfghybdf";
String lrs="";
int n = str.length();
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
String x = lcp(str.substring(i,n),str.substring(j,n));
if(x.length() > lrs.length()) lrs=x;
}
}
System.out.println("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 = str.length();
Permutation permutation = new Permutation();
permutation.permute(str, 0, n-1);
}
private void permute(String str, int l, int r)
{
if (l == r)
System.out.println(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 = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(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 = str1.replaceAll("\\s+", "");

System.out.println("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 = a.toUpperCase();
System.out.println(a1);
}
}
Output
------
JAVA

public class Sample{


public static void main(String[] args) {

String a = "JAVA";
String a1 = a.toLowerCase();
System.out.println(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 = a.replace(" ","$");
System.out.println(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=a.length()-1;i>=0;i--){
char ch = a.charAt(i);
a2= a2+ch;
}
System.out.println(a2);
if(a.equals(a2)) {
System.out.println("Palindrome String");
}else {
System.out.println(" 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 = name.length() - 1; i >= 0; i--) {
char ch = name.charAt(i);
res = res + ch;
}
System.out.println(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 = name.toCharArray();
for (char c : ch) {
if (emp.containsKey(c)) {
int count = emp.get(c);
emp.put(c, count + 1);
} else {
emp.put(c, 1);

}
System.out.println(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 < string.length(); i++){

if(string.charAt(i) != ' '){


word = word + string.charAt(i);
}
else{

words[length] = word;

length++;

word = "";
}
}
small = large = words[0];

for(int k = 0; k < length; k++){

if(small.length() > words[k].length())


small = words[k];

if(large.length() < words[k].length())


large = words[k];
}
System.out.println("Smallest word: " + small);
System.out.println("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 ";

System.out.println(
"Individual characters from given string: ");

for (int i = 0; i < string.length(); i++) {


System.out.print(string.charAt(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(System.in);

System.out.println("Enter a sentence");
sen = sc.nextLine();
sen=sen+" ";
sen=sen.toLowerCase();
len=sen.length();
smallestPalindrome=sen;
for(i=0;i< len;i++)
{
ch=sen.charAt(i);
if(ch==' ')
{

if(wd.equals(wd1)==true)
{
if(wd.length()>largestPalindrome.length())
{
largestPalindrome=wd;
}
if(wd.length()< smallestPalindrome.length())
{
smallestPalindrome=wd;
}
}
wd1="";
wd="";
}
else
{
wd=wd+ch;
wd1=ch+wd1;
}
}
System.out.println("Largest Palindrome words in
sentence:"+largestPalindrome);
System.out.println("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(System.in);
System.out.print("Original string : ");

String originalStr = scanner.nextLine();


scanner.close();

String words[] = originalStr.split("\\s");


String reversedString = "";

for (int i = 0; i < words.length; i++)


{
String word = words[i];
String reverseWord = "";
for (int j = word.length() - 1; j >= 0; j--) {
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}

// Displaying the string after reverse


System.out.print("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 = name.length() - 1; i >= 0; i--) {
char ch = name.charAt(i);
res = res + ch;
}
System.out.println(res);
}
}

You might also like