Here you can find all type of String Operation programs (if i miss any please send me in Here)
Program: 1 Reverse a string with out using string lib functions.
Program: Even Odd Number Example
Output:
Output:
Program: Factorial Using RecursionProgram: 1 Reverse a string with out using string lib functions.
public class HelloWorld{
public static void main(String []args){
String str = "BREJESH";
int l=str.length();
char[] ch=str.toCharArray();
char t='\0';
System.out.println("Original String is: "+str);
for(int i=0;i<(str.length()/2);i++){
t=ch[l-1];
ch[l-1]=ch[i];
ch[i]=t;
l--;
}
System.out.println("Reverse String is: "+String.valueOf(ch));
}
}
Output:Original String is: BREJESH
Reverse String is: HSEJERB
Program: 2 Arrange String characters in alphabet order.public class HelloWorld{
public static void main(String []args){
String str = "BREJESH";
int l=str.length();
char[] ch=str.toCharArray(); char t='\0';
System.out.println("Original String is: "+str);
for(int i=0;i<str.length();i++){
for(int j=i;j<str.length();j++){
if(ch[i] > ch[j]){
t=ch[i];
ch[i]=ch[j];
ch[j]=t;
}
}
}
System.out.println("Alphabet Order String is: "+String.valueOf(ch));
}
}
Output:Original String is: BREJESH
Alphabet Order String is: BEEHJRS
Program: 3 Find Duplicate characters in given stringpublic class HelloWorld{
public static void main(String []args){
String str = "BREJESH";
str = str.toLowerCase();
int l=str.length();
char[] ch=str.toCharArray(); int[] dup=new int[26];
char t='\0';
System.out.println("Original String is: "+str);
for(int i=0;i<str.length();i++){
if(dup[ch[i]-97] == -1){
System.out.println("Duplicate found: " +ch[i]);
}
else
dup[ch[i]-97]=-1;
}
}
}
Output:Original String is: brejesh
Duplicate found: e
Program: 4 Print the characters count in the given stringpublic class HelloWorld{
public static void main(String []args){
String str = "BREJESH";
str = str.toLowerCase();
int l=str.length();
char[] ch=str.toCharArray(); int[] dup=new int[26];
char t='\0';
System.out.println("Original String is: "+str);
for(int i=0;i<str.length();i++){
dup[ch[i]-97] = dup[ch[i]-97] + 1;
}
System.out.println("Char count: ");
for(int i=0;i<26;i++){
if(dup[i]!=0) System.out.println((char)(i+97) + " - " + dup[i]);
}
}
}
Output:Original String is: brejesh
Char count:
b - 1
e - 2
h - 1
j - 1
r - 1
s - 1
Program: 5 String Palindromepublic class HelloWorld{
public static void main(String []args){
String str = "BREJESH";
int l=str.length(), f=0;
char[] ch=str.toCharArray();
System.out.println("Original String is: "+str);
for(int i=0;i<(str.length()/2);i++){
if(ch[i] != ch[l-1])
f=1;
l--;
}
if(f==1)
System.out.println(str + " is not a palindrome");
else
System.out.println(str + " is a palindrome");
}
}
Output:Original String is: ABCBA
ABCBA is a palindrome
Program: Even Odd Number Example
public class FindEvenOrOddNumber { public static void main(String[] args) { int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10}; for(int i=0; i < numbers.length; i++){ if(numbers[i]%2 == 0) System.out.println(numbers[i] + " is even number."); else System.out.println(numbers[i] + " is odd number."); } } }
Output:
1 is odd number. 2 is even number. 3 is odd number. 4 is even number. 5 is odd number. 6 is even number. 7 is odd number. 8 is even number. 9 is odd number. 10 is even number.Program: Find Largest and Smallest Number in given array
public class FindLargestSmallestNumber { public static void main(String[] args) { int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23}; int smallest = numbers[0]; int largetst = numbers[0]; for(int i=1; i< numbers.length; i++){ if(numbers[i] > largetst) largetst = numbers[i]; else if (numbers[i] < smallest) smallest = numbers[i]; } System.out.println("Largest Number is : " + largetst); System.out.println("Smallest Number is : " + smallest); } }Output:
Largest Number is : 98 Smallest Number is : 23Program: Factorial
public class NumberFactorial { public static void main(String[] args) { int number = 5; int factorial = number; for(int i =(number - 1); i > 1; i--){ factorial = factorial * i; } System.out.println("Factorial of a number is " + factorial); } }
Output:
Factorial of a number is 120
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class JavaFactorialUsingRecursion { public static void main(String args[]) throws NumberFormatException, IOException{ System.out.println("Enter the number: "); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(br.readLine()); int result= fact(a); System.out.println("Factorial of the number is: " + result); } static int fact(int b) { if(b <= 1) return 1; else return b * fact(b-1); } }Output:
Enter the number: 5 Factorial of the number is: 120Program: Reverse Number
public class ReverseNumber { public static void main(String[] args) { int number = 1234; int reversedNumber = 0; int temp = 0; while(number > 0){ temp = number%10; reversedNumber = reversedNumber * 10 + temp; number = number/10; } System.out.println("Reversed Number is: " + reversedNumber); } }Output:
Reversed Number is: 4321
Program: Swap Numberspublic class SwapElementsExample { public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); swap(num1, num2); } private static void swap(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; System.out.println("After Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); } }
Output:
Before Swapping Value of num1 is :10 Value of num2 is :20 After Swapping Value of num1 is :20 Value of num2 is :10Program: Swap Numbers Without Using Third Variable
public class SwapElementsWithoutThirdVariableExample { public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); } }Output:
Before Swapping Value of num1 is :10 Value of num2 is :20 Before Swapping Value of num1 is :20 Value of num2 is :10
can to post "how to get a string from a user using scanner and reversing it using for loop and printing it"??
ReplyDeleteHi Robby,
ReplyDeletePlease find your requested code here..
http://corejavacorner.blogspot.com/2013/11/get-input-string-from-user-using.html
Thanks for visiting my blog...
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteJava Training in Electronic city