all jav programs of string
all jav programs of string
// Main Function
public static void main(String args[])
{
String str = new String("example");
System.out.println(str);
}
}
Output
Example
Here's a list of commonly used String functions in Java, each with a simple example, its
definition, and a brief explanation of its use:
1. length()
2. charAt(index)
Definition: Extracts a portion of the string from start index to end index (exclusive).
Use: To get a specific part of a string.
4. toUpperCase()
5. toLowerCase()
6. trim()
7. replace(oldChar, newChar)
8. equals(otherString)
9. contains(sequence)
10. split(delimiter)
Definition: Splits the string into an array of substrings based on the delimiter.
Use: To break a string into parts for processing.
11. indexOf(char)
Definition: Returns the index of the first occurrence of the specified character.
Use: To locate a character in a string.
12. isEmpty()
Answer
1. Write a Java program to accept a string from the user and perform the following operations:
2. Write a Java program that demonstrates the use of the following Math class methods:
Math.pow()
Math.sqrt()
Math.abs()
Math.ceil()
Math.floor()
3.Write a Java program to accept an array of integers from the user. The program should
calculate and display the following:
Write a main method to create an object of the class and call the above member methods.
5. Write a Java program to print the below pyramid pattern for n rows.
*
***
*****
*******
*********
1. Reverse a String:
Write a Java program to reverse a given string without using built-in functions.
Example:
Input: "hello"
Output: "olleh"
2. Find the Largest Element in an Array:
Write a Java program to find the largest element in an array of integers.
Example:
Input: [3, 7, 1, 8, 4]
Output: 8
3. Check if a Number is Prime:
Write a Java program to check if a given number is prime.
Example:
Input: 7
Output: Prime
4. Factorial of a Number:
Write a Java program to calculate the factorial of a given number using recursion.
Example:
Input: 5
Output: 120
5. Fibonacci Series:
Write a Java program to print the first n numbers of the Fibonacci series.
Example:
Input: n = 5
Output: 0, 1, 1, 2, 3