[Go to site: main page, start]

0% found this document useful (0 votes)
179 views4 pages

Java String Programs and Examples

Uploaded by

biswasamaresh421
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views4 pages

Java String Programs and Examples

Uploaded by

biswasamaresh421
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programs Based on Strings:

1. Write a Java program to accept a word or a string and display a new string after
removing all vowels.
Sample Input: COMPUTER APPLICATIONS
Sample Output: CMPTR PPLCTNS
2. Write a Java program to enter a string/sentence and display the longest word along
with its length.
Sample Input: "TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN"
Sample Output: The longest word: FOOTBALL, The length of the word: 8
3. Write a Java program to accept a string in uppercase and replace all vowels with an
asterisk ().
Sample Input: "TATA STEEL IS IN JAMSHEDPUR"
Sample Output: TT* ST**L S N JMSHDP*R
4. Write a Java program to enter a sentence, extract the first letter of each word, and
form a new word.
Sample Input: Vital Information Resource Under Seize
Sample Output: VIRUS
5. Write a Java program to enter a sentence and display only the words that are
palindromes.
Sample Input: MOM AND DAD ARE NOT AT HOME
Sample Output: MOM, DAD
6. Write a Java program to reverse the order of words in a given sentence.
Sample Input: Computer is Fun
Sample Output: Fun is Computer
7. Write a Java program to input a sentence and display the word containing the
maximum number of vowels.
Sample Input: HAPPY NEW YEAR
Sample Output: The word with maximum vowels: YEAR
8. Write a Java program to accept a word, convert it to lowercase (if in uppercase), and
replace only vowels with their next letter.
Sample Input: COMPUTER
Sample Output: CPMPVTFR
9. Write a Java program to input a sentence, convert it to uppercase, and count words
starting with the letter 'A'.
Sample Input: advancement and application of information technology are ever
changing
Sample Output: ADVANCEMENT AND APPLICATION OF INFORMATION
TECHNOLOGY ARE EVER CHANGING
Total words starting with 'A': 4
10. Write a Java program to input a sentence, convert it to uppercase, and display each
word separately.
Sample Input: India is my country
Sample Output:
INDIA
IS
MY
COUNTRY
11. Write a Java program to accept two strings of the same length and form a new word
by interleaving their characters.
Sample Input: BALL, WORD
Sample Output: BWAOLRLD
12. Write a Java program to accept a string, convert it to uppercase, and count double-
letter sequences.
Sample Input: "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE"
Sample Output: 4
13. Write a Java program to check whether a given word is a palindrome, a special word
(starts and ends with the same letter), or neither.
Example Special Words: EXISTENCE, COMIC, WINDOW
Example Palindromes: MALAYALAM, MADAM, LEVEL, ROTATOR, CIVIC
14. Write a Java program to input a sentence, convert it to uppercase, and display words
containing at least one pair of consecutive letters.
Sample Input: MODEM IS AN ELECTRONIC DEVICE
Sample Output: MODEM, DEVICE
Total words with consecutive letters: 2
15. Write a Java program to accept a word and display patterns:
(a) BLUEJ → BLUE → BLU → BL → B
(b) J → EE → UUU → LLLL → BBBBB
(c) BLUEJ → LUEJ → UEJ → EJ
16. Write a Java program to generate a triangle or an inverted triangle based on user
choice.
Example 1:
Input: Type 1 for a triangle, Type 2 for an inverted triangle
Choice: 1
Word: BLUEJ
Output:
B
LL
UUU
EEEE
JJJJJ
Example 2:
Choice: 2
Word: BLUEJ
Output:
BLUEJ
BLUE
BLU
BL
B
17. Write a menu-driven Java program using a switch statement:
(a) Print Floyd's Triangle:
Example Output:
1
23
456
7 8 9 10
11 12 13 14 15
(b) Display the pattern:
I
IC
ICS
ICSE
For invalid choices, display an error message.
18. Define a class to accept ten characters in an array and perform a linear search for a
given character.
If found, display the character and its index.
If not found, display "No such character is available in the array".
19. Write a Java program to store names of ten countries and their capitals in separate
arrays. Display only the countries that start with a vowel along with their capitals.
Example Output:
Countries Name | Capital
XXXX | XXXX
XXXX | XXXX
20. Write a Java program to store 20 names and telephone numbers in two separate
arrays. Sort names alphabetically using the selection sort technique and display them
along with corresponding phone numbers.
21. Write a Java program to accept 10 names in an array and display names that start
with a user-input letter.
Sample Input:
Aman Shahi
Akash Gupta
Suman Mishra
Sample Output (if input letter is 'A'):
Aman Shahi
Akash Gupta

Common questions

Powered by AI

The complexity of constructing pattern displays using nested loops involves understanding multiple levels of iteration. Each outer loop controls a major aspect of the pattern, such as rows or cascading lengths; each inner loop manages the character display per the pattern's requirements. Logical conditions influence control flow to adjust patterns dynamically. The complexity increases with design intricacy; thus, one must consider loop depth and condition structure to maintain efficiency while ensuring backward/forward compatibility in pattern adjustment .

To optimize consecutive character sequence processing in Java, one can utilize efficient traversal techniques such as sliding window or pointers to minimize redundant checks. Using these methods, the program can maintain counters or flags to track consecutive sequences. Proper use of data structures like arrays and conditions enriches the algorithm’s performance when identifying sequences like double letters in strings . Empirical testing and refinement of conditions (e.g., only increment counters at detected pairs) streamline operations.

To identify palindrome words in sentences using Java, an algorithm can be designed to split the sentence into words, then check each word by reversing it and comparing it to the original. If the word and its reverse are identical, it is a palindrome. This method leverages Java's string manipulation capabilities, such as using `StringBuilder` to reverse words .

Java provides efficient techniques such as the `toUpperCase()` and `toLowerCase()` methods for case conversion, and methods such as `split()` for parsing sentences into words. For analyzing word structure, loops and condition checking can be used to identify conditions like starting letters or vowel counts. Efficient parsing and conditional checking allow for operations like counting words that meet specific structural criteria, such as starting with a specific letter .

Maintaining data relationships while sorting in Java can be achieved by using parallel arrays or custom data structures. By applying a sorting algorithm such as selection sort on one array (e.g., names) and simultaneously sorting the corresponding entries in a secondary array (e.g., telephone numbers), integrity is maintained. Using a custom class encapsulating both fields and implementing `Comparable` or using `Comparator` can provide a more integrated approach for sorting based on one attribute while preserving the relationship .

Robust user input handling in case-sensitive operations can be achieved by normalizing inputs through methods like `toUpperCase()` or `toLowerCase()` before processing, thereby eliminating case-related discrepancies. Using scanners with built-in exception handling, inputs can be validated and erroneous data filtered out early in the process. Programs need string manipulation methods for consistent word extraction and counting, leveraging conditions like evaluating starting characters efficiently to maintain integrity across varied inputs .

Designing a Java program to extract first letters involves careful consideration of parse-edge cases such as multiple spaces, punctuation, or empty inputs. Using `split()` with appropriate delimiters helps manage word extraction, while conditions ensure non-letter characters don’t lead to erroneous extraction. Common errors to avoid include indexing errors with strings, disregarding sentences that could generate invalid outputs, and ensuring input trimming to handle unintended leading/trailing spaces .

Interleaving characters from two equal-length strings to form a composite word involves iterating over the characters of both strings simultaneously, extracting one character and concatenating it with the corresponding character from the other string. This can be achieved using a for-loop that iterates through the index range of the strings, adding alternating characters to form the new word. It demonstrates the use of loop control and string indexing capabilities of Java .

Java can generate and display word-based patterns in geometric shapes by utilizing loops and conditionals to manipulate string formats. For example, creating triangular and inverted triangular displays can involve iteratively printing portions of a string in a specific sequence to achieve the desired shape, such as displaying a word in descending or ascending character order . This involves careful control of print statements and loop boundaries to arrange characters as required.

Java programs can manipulate strings to remove vowels using methods such as replacing each vowel character with an empty string and outputting the modified string. For example, a program can create a pattern by interleaving two strings of the same length by taking characters alternately from each string . Such manipulations require understanding string operations and conditions for consistent pattern development.

You might also like