ASM - CoreJava Strings - 1
String Assignments - 1
1. Reverse
a String: Write a Java program to reverse a given string.
Algorithm:
·
Initialize an empty string to store the reversed
result.
·
Traverse the input string from end to start and
append each character to the result string.
2. Count
Vowels and Consonants: Write a Java program to count the number of vowels
and consonants in a given string.
Algorithm:
·
Initialize counters for vowels and consonants to
zero.
·
Iterate through each character in the string.
·
Check if the character is a vowel (a, e, i, o,
u) and increment the vowel count, otherwise increment the consonant count.
3. Check
for Palindrome: Write a Java program to check if a given string is a
palindrome.
Algorithm:
·
Remove spaces and convert the string to
lowercase.
·
Compare the original string with its reverse to
check if it's a palindrome.
4. Count
Words in a Sentence: Write a Java program to count the number of words in a
given sentence.
Algorithm:
·
Split the input sentence into words using
whitespace as a delimiter.
·
Count the number of words in the resulting
array.
5. Concatenate
Strings: Write a Java program to concatenate two strings without using the +
operator.
Algorithm:
·
Use a StringBuilder or StringBuffer to append
the second string to the first.
6. Remove
Duplicates from a String: Write a Java program to remove duplicate
characters from a string.
·
Algorithm:
·
Use a Set to keep track of unique characters
while iterating through the string.
Check for Anagrams: Write a Java program to check
if two strings are anagrams.
Algorithm:
·
Remove spaces and convert both strings to
lowercase.
·
Sort the characters in both strings and compare
them.
7. Find
the Most Common Character: Write a Java program to find the most common
character in a string.
Algorithm:
·
Use a HashMap to store character frequencies and
find the character with the highest frequency.
8. Reverse
Words in a Sentence: Write a Java program to reverse the words in a given
sentence.
Algorithm:
·
Split the sentence into words and reverse their
order.
9. Count
Occurrences of a Substring: Write a Java program to count the occurrences
of a substring in a given string.
Algorithm:
·
Use the indexOf method in a loop to find
all occurrences of the substring.
10. Check
for Rotation: Write a Java program to check if one string is a rotation of
another.
Algorithm:
·
Concatenate the first string with itself and
check if the second string is a substring of the result.
11. String
Compression: Write a Java program to compress a string by replacing
repeated characters with a count.
Algorithm:
·
Iterate through the string, counting consecutive
characters and appending the count to the result.
12. String
to Integer Conversion: Write a Java program to convert a string containing
an integer to an actual integer.
Algorithm:
·
Iterate through the characters, converting them
to integers and accumulating the result.
13. Remove
Specific Characters: Write a Java program to remove all occurrences of a
specific character from a string.
Algorithm:
Use a StringBuilder or StringBuffer to build the result
string, excluding the specified character.
14. Generate
Random Password: Write a Java program to generate a random password of a
specified length.
Algorithm:
·
Create a pool of characters (letters, digits,
symbols) and randomly select characters for the password.
15. Check
for Subsequence: Write a Java program to check if one string is a
subsequence of another.
Algorithm:
·
Use two pointers to traverse both strings and
check if the characters match.
16. String
Permutations: Write a Java program to generate all permutations of a given
string.
Algorithm:
·
Use a recursive approach to generate
permutations by swapping characters.
17. String
Splitting: Write a Java program to split a string into multiple lines with
a maximum line length.
Algorithm:
·
Iterate through the string, splitting it into
lines not exceeding the maximum length.
18. Character
Frequency Count: Write a Java program to count the frequency of each
character in a string and display the results.
Algorithm:
·
Use a HashMap to store character frequencies and
display the results.
19. Remove
Extra Spaces: Write a Java program to remove extra spaces from a string.
Algorithm:
·
Replace multiple consecutive spaces with a
single space using regular expressions.
20. CamelCase
to Underscore: Write a Java program to convert a CamelCase string to an
underscore-separated string.
Algorithm:
Iterate through the string and insert underscores before
uppercase letters.
21. String
Encryption: Write a Java program to encrypt a string using a simple
substitution cipher.
Algorithm:
·
Define a mapping from each character to another
character and replace characters accordingly.
22. String
Matching: Write a Java program to find all occurrences of a pattern in a
given text.
Algorithm:
·
Use a loop and the indexOf method to find
all occurrences of the pattern.
23. String
Rotation Detection: Write a Java program to detect if one string is a
rotation of another using the contains method.
Algorithm:
·
Concatenate the first string with itself and
check if it contains the second string.
24. String
Comparison with Wildcard: Write a Java program to compare two strings with
wildcard characters (* and ?).
Algorithm:
·
Implement a custom pattern matching algorithm
that handles wildcard characters.
25. Shuffle
Characters in a String: Write a Java program to randomly shuffle the
characters in a given string.
Algorithm:
·
Convert the string to a character array and use
a random number generator to shuffle the characters.
26. Common
Characters in Two Strings: Write a Java program to find the common
characters between two strings.
Algorithm:
·
Use two arrays to count the frequency of
characters in each string and find common characters.
27. Find
Anagram Groups in a List of Strings: Write a Java program to group anagrams
from a list of strings.
Algorithm:
·
Use a HashMap where the key is a sorted version
of a word, and the value is a list of anagrams.
28. Find
All Palindromic Substrings: Write a Java program to find all palindromic
substrings within a given string.
Algorithm:
·
Use dynamic programming or expand around the centre
to identify all palindromic substrings.
29. Reverse
Words in a Sentence with an Array: Write a Java program to reverse the
words in a given sentence while preserving the word order using arrays.
Algorithm:
·
Split the sentence into words and store them in
an array.
·
Reverse the array containing the words.
·
Reconstruct the sentence by joining the reversed
words with spaces.
30. Count
Substrings with Exactly K Distinct Characters: Write a Java program to
count the number of substrings in a string with exactly K distinct characters.
Algorithm:
·
Use a sliding window approach to count
substrings with exactly K distinct characters.
31. Longest
Common Prefix of Multiple Strings: Write a Java program to find the longest
common prefix among an array of strings.
Algorithm:
·
Find the shortest string in the array.
·
Compare each character of the shortest string
with the other strings in the array.
32. Compress
String Using Array: Write a Java program to compress a string by replacing
repeated characters with a count using arrays.
Algorithm:
·
Iterate through the string, using an array to
count consecutive characters.
·
Append the character and its count to the result
string.
33. Largest
Common Substring: Write a Java program to find the largest common substring
in an array of strings.
Algorithm:
·
Compare each string with the others to find the
largest common substring.
34. Find
the Longest Word Formed by Other Words: Write a Java program to find the
longest word in an array of words that can be formed by concatenating other
words in the array.
Algorithm:
·
Sort the array of words by length.
·
Check if each word can be formed using shorter
words in the array.
35. Longest
Substring Without Repeating Characters: Write a Java program to find the
length of the longest substring without repeating characters in a given string.
Algorithm:
1.
Initialize variables start and maxLen
to 0.
2.
Create a HashSet to store unique characters.
3.
Iterate through the string with a pointer end.
4.
If the character at end is not in the
HashSet, add it and update maxLen as maxLen = max(maxLen, end - start
+ 1).
5.
If the character at end is in the
HashSet, remove characters from the start of the substring until the
repeated character is removed.
6.
Increment start pointer to continue
searching for a longer substring.
7.
Repeat steps 3-6 until end reaches the
end of the string.
36. String
Parsing: Write a Java program to parse a string containing key-value pairs
(e.g., "name=John age=30").
Algorithm:
1.
Split the input string by spaces to obtain an
array of key-value pairs.
2.
Create a HashMap to store the parsed key-value
pairs.
3.
Iterate through the array of pairs.
4.
Split each pair by the '=' sign to separate the
key and value.
5.
Store the key and value in the HashMap.
37. String
to Morse Code Conversion: Write a Java program to convert a string to Morse
code.
Algorithm:
1.
Create a mapping of characters to their Morse
code representations.
2.
Initialize an empty string to store the Morse
code result.
3.
Iterate through each character in the input
string.
4.
Convert the character to its Morse code
representation using the mapping.
5.
Append the Morse code representation to the
result string.
38. String
Matching with Regular Expressions: Write a Java program to match a string
against a regular expression pattern.
Algorithm:
1.
Create a regular expression pattern using the Pattern.compile
method.
2.
Create a Matcher object by applying the
pattern to the input string.
3.
Use the find method of the Matcher
to search for matches.
4.
Retrieve and work with the matched substrings as
needed.
39. String
Truncation: Write a Java program to truncate a string to a specified
maximum length, adding ellipses if necessary.
Algorithm:
1.
Check if the length of the input string is less
than or equal to the specified maximum length.
2.
If the string is within the length limit, no truncation
is needed.
3.
If the string is longer, truncate it to the
maximum length.
4.
Add ellipses ("...") to the truncated
string to indicate truncation.
40. String
Encryption with Key: Write a Java program to encrypt a string using a
provided encryption key.
Algorithm:
1.
Define an encryption algorithm that takes the
input string and the encryption key.
2.
Apply the encryption algorithm to transform the
input string using the provided key.
3.
Return the encrypted string as the result.
41. String
Comparison with Levenshtein Distance: Write a Java program to compare two
strings based on their Levenshtein distance (edit distance).
Algorithm:
1.
Implement the Levenshtein distance algorithm to
calculate the minimum number of edits (insertions, deletions, substitutions)
needed to transform one string into another.
2.
Compare the Levenshtein distance between the two
strings to determine their similarity.
42. String
Pattern Matching Using Wildcards: Write a Java program to match a string
against a pattern that includes wildcards (* and ?).
Algorithm:
1.
Implement a custom pattern matching algorithm
that handles wildcards (*) and (?).
2.
Iterate through the pattern and input string to
match characters and wildcards as needed.
3.
Return the result of the pattern match.
43. String
Case Conversion: Write a Java program to convert a string to uppercase and
lowercase.
·
Algorithm:
1.
Use the toUpperCase method to convert the
string to uppercase.
2.
Use the toLowerCase method to convert the
string to lowercase.
44. String
to Hexadecimal Conversion: Write a Java program to convert a string to its
hexadecimal representation.
Algorithm:
1.
Iterate through each character in the input
string.
2.
Convert each character to its hexadecimal
representation using the Integer.toHexString method.
3.
Append the hexadecimal representation to the
result string.
Comments
Post a Comment