Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Java

The document provides Java programs for reversing a string using both inbuilt functions (StringBuilder) and manual methods, checking if a string is a palindrome, counting character occurrences, finding string length without the length() method, determining if a number is odd or even, and checking if a number is prime. Each program includes code snippets and explanations of their functionality. The examples demonstrate basic string manipulation and number checking techniques in Java.

Uploaded by

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

Java

The document provides Java programs for reversing a string using both inbuilt functions (StringBuilder) and manual methods, checking if a string is a palindrome, counting character occurrences, finding string length without the length() method, determining if a number is odd or even, and checking if a number is prime. Each program includes code snippets and explanations of their functionality. The examples demonstrate basic string manipulation and number checking techniques in Java.

Uploaded by

snehaldivekar77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Reverse a string program in java by using inbuilt function and without

using inbuilt function

1. Using Inbuilt Function (StringBuilder):


Java provides the StringBuilder class, which has a reverse() method. Here's how
you can reverse a string using it:
public class ReverseString {
public static void main(String[] args) {
String str = "Hello World";

// Using StringBuilder's reverse method


String reversed = new StringBuilder(str).reverse().toString();

System.out.println("Reversed String: " + reversed);


}
}
Output:
Reversed String: dlroW olleH

2. Without Using Inbuilt Function:


If you want to reverse the string manually, you can loop through the string and
append characters in reverse order. Here's how you can do that:
public class ReverseString {
public static void main(String[] args) {
String str = "Hello World";

// Without using inbuilt functions


String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i); // Appending characters from the end to the
beginning
}
System.out.println("Reversed String: " + reversed);
}
}
Output:
Reversed String: dlroW olleH

2. Check Palindrome String


A palindrome is a string that reads the same backward as forward (e.g.,
"madam").

public class Palindrome {


public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if(str.equals(reversed)) {
System.out.println(str + " is a palindrome");
} else {
System.out.println(str + " is not a palindrome");
}
}
}

3. Count Occurrence of a Character in a String


Counting how many times a particular character appears in a string.
Code:
public class CountCharacter {
public static void main(String[] args) {
String str = "programming";
char ch = 'g';
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
System.out.println("Character '" + ch + "' appears " + count + " times.");
}
}

7. Find Length of a String Without Using length() Method


This program counts the length of a string manually.

public class StringLength {


public static void main(String[] args) {
String str = "hello";
int length = 0;

// Loop through the string to count characters


while (str.charAt(length) != '\0') {
length++;
}

System.out.println("Length of the string: " + length);


}
}

// Java Program to Check if Given Integer is Odd or Even


import java.util.Scanner;

public class EvenOdd {


public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Check if the number is even or odd


if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}

// Close the scanner object to prevent resource leak


scanner.close();
}
}

Prime Number:
import java.util.Scanner;

public class PrimeNumber {


public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);

// Ask the user to enter a number


System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Assume the number is prime


boolean isPrime = true;

// Check if the number is divisible by any number from 2 to number-1


for (int i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false; // It's not prime if divisible by any number
break; // No need to check further
}
}

// Output result
if (number <= 1) {
isPrime = false; // Numbers less than or equal to 1 are not prime
}

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}

// Close the scanner object to prevent resource leak


scanner.close();
}
}

You might also like