Java String Programs: Code Examples & Solutions

Java Programming Examples - Strings

In Java programming, a string is an object that represents a sequence of characters. Strings are created and manipulated using the String class. A string can be created in Java using either a string literal or a new keyword. StringBuffer and StringBuilder implement the CharSequence in Java to generate new strings. Java String class has many methods to perform operations on strings such as concat(), compare(), split(), equals(), length(), replace(), matches(), trim(), compareTo(), substring(), intern() etc.

The following section contains various programs on strings, string operations, string matching, approximate string matching, and encryption algorithms. Each sample program includes a program description, Java code, and program output. All examples have been compiled and tested on Windows and Linux systems.

Here is the listing of Java programming examples on Strings:

  1. Java Programs on String Operations
  2. Java Programs on Strings
  3. Java Programs on String Matching
  4. Java Programs on Approximate String Matching
  5. Java Programs on Cryptography
  6. Java Programs on Longest Common SubString/SubSequence

1. Java Programs on String Operations

Program Description
String Concatenation in Java Java Program to Concatenate Two Strings
Input: Enter First String: Hello
Enter Second String: World
Output: Hello World
Remove Characters from Input String in Java Java Program to Remove Characters from the Input String which are Present in the Mask String
Input: Enter String: Practice Java String Programs
Enter Mask String: abc
Output: Prtie Jv String Progrms
Delete Adjacent Duplicates from String in Java Java Program to Remove All Adjacent Duplicates from String
Input: String: ABBCCCD
Output: ACD
Reverse Each Word in a String in Java Java Program to Reverse Each Word in a String
Input: String: Java Programs on String
Output: avaj smargorp no gnirts
Convert String to Integer Array in Java Java Program to Convert a String to an Integer Array
Input: Enter the string: abc pqr xyz
Output: -1 -1 -1

advertisement
advertisement

2. Java Programs on Strings

Program Description
Largest & Smallest Word in a String in Java Java Program to Find the Largest and Smallest Word in a String
Input: Enter String: String Programs in Java
Output: The largest and smallest word is “Programs” and “in”
First Non Repeating Character in a String in Java Java Program to Find the First Non-repeated Character in a String
Input: Enter String: Welcome to Sanfoundry
Output: First Non-Repeating Character is W
Length of Longest Repeating Subsequence in Java Java Program to Find the Length of Longest Repeating Sequence in a String
Longest Common Substring in Java Java Program to Find Length of Longest Common Substring
Print Longest Substring without Repeating Characters in Java Java Program to Find the Longest SubString Without Any Repeated Characters
Count Unique Words from a String in Java Java Program to Count the Number of Unique Words
Input: Enter String: Java is great C++ is also great
Output: Total number of unique words are 3
Count Vowels, Uppercase, Lowercase & Special Characters from a String in Java Java Program to Find the First Capital Letter/Small Letter/Vowel/Consonant/Number/Whitespace/Special Character in a Given String
Permutation of a String in Java Java Program to Permute All Letters of an Input String
Check if Two Strings are Permutation of Each Other in Java Java Program to Check if Two Strings are Cyclic Permutations of Each Other
String Palindrome Program in Java Java Program to Check if a Given String is Palindrome
Input: Enter String: NeveroddorEVen
Output: The string is palindrome
Anagram Palindrome in Java Java Program to Check if Any Anagram of a String is Palindrome or Not
Switch Case String in Java Java Program to Implement Switch Statement on Strings
Grep, Egrep & Fgrep in Java Java Program to Implement Grep, Egrep and Fgrep Commands

3. Java Programs on String Matching

Program Description
Naive String Matching in Java Java Program to Perform Naive String Matching
Aho-Corasick Algorithm in Java Java Program to Implement Aho Corasick Algorithm for Pattern Searching
Rabin Karp String Matching Algorithm in Java Java Program to Implement Rabin-Karp Method for Pattern Searching
Manacher Algorithm in Java Java Program to Implement Manacher Algorithm
Wagner Fischer Algorithm in Java Java Program to Implement Wagner Fischer Algorithm
String Matching Using Library in Java Java Program to Perform String Matching Using String Library
String Matching Using Vectors in Java Java Program to Perform String Matching Using Vectors
String Search Algorithm in Java Java Program to Implement String Search Algorithm for Short Text Sizes
Repeated Search Pattern in Java Java Program to Repeatedly Search the Same Text

4. Java Programs on Approximate String Matching

Program Description
Approximate String Matching using DP in Java Java Program to Solve Approximate String Matching using Dynamic Programming
Online String Matching using Wagner & Fisher in Java Java Program to Implement Wagner and Fisher Algorithm for Online String Matching
Levenshtein Distance Computing Algorithm in Java Java Program to Implement Levenshtein Distance Computing Algorithm

5. Java Programs on Cryptography

Program Description
Monoalphabetic Cypher in Java Java Program to Implement the Monoalphabetic Cipher
Input: Enter the message: Sanfoundry
Output: Encrypted message: LQFYGXFRKN; Decrypted message: sanfoundry
Caesar Cipher in Java Java Program to Implement Caesar Cipher
Input: Enter the String for Encryption: Sanfoundry
Output: vdqirxqgub
Vigenere Cipher in Java Java Program to Implement the Vigenere Cipher
Hill Cipher in Java Java Program to Implement the Hill Cipher
Affine Cipher in Java Java Program to Implement Affine Cipher
Playfair Cipher Encryption Program in Java Java Program to Encrypt Message using Playfair Cipher
Playfair Cipher Decryption Program in Java Java Program to Decrypt Message using Playfair Cipher
One Time Pad Algorithm in Java Java Program to Implement the One Time Pad Algorithm
RSA Algorithm in Java Java Program to Implement the RSA Algorithm
MD5 Algorithm in Java Java Program to Implement the MD5 Algorithm
Transposition Technique Program in Java Java Program to Perform Cryptography using Transposition Technique
Input: Encrypted Message: f*o*n*ayn*d*Sru*
Output: Decrypted Message: Sanfoundry
Checksum Program in Java Java Program to Implement the Checksum Method for Small String Messages

6. Java Programs on Longest Common SubString/SubSequence

Program Description
Longest Increasing Subsequence in Java Java Program to Find the Longest Increasing Subsequence
Longest Common Subsequence Algorithm in Java Java Program to Implement Longest Common Subsequence Algorithm
Longest Common Subsequence in Java Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.