Java program to find out Repeated characters in a String

Java program to find out Repeated characters in a String.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package logic;
 
import java.util.ArrayList;
import java.util.Scanner;
 
public class Stringrepeat 
{
 
public static void main(String[] args) 
{
 
	 System.out.println("enter the string ");
	 Scanner s = new Scanner(System.in);
 
	 String b=s.next();
 
	  for(int i=0;i<b.length()-1;i++)
	   {
		  for(int j=i;j<b.length()-1;j++)
		  { 
 
	            if(b.charAt(i)==b.charAt(j+1))
	            {
		     System.out.println("repeating chararcter is "+b.charAt(i));
	           }
	         }
	   }
	}
    }

output:

1
2
3
4
5
enter the string 
aabbgjg
repeating chararcter is a
repeating chararcter is b
repeating chararcter is g

How to Swap two numbers without using third variable in Java

How to Swap two numbers without using third variable in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package logic;
 
import java.util.Scanner;
 
public class SwapWithoutThird 
{
	 static void swap(int arg1,int arg2)
	 {
		 arg1=arg1+arg2;
		 arg2=arg1-arg2;
		 arg1=arg1-arg2;
 
		 System.out.println("after Swapin value of n1= "+arg1);
		 System.out.println("after Swapin value of n2= "+arg2);
 
	 }
 
	public static void main(String[] args) 
	{
		Scanner c=new Scanner(System.in);
		System.out.println("enter a first number");
		int n1=c.nextInt();
 
		System.out.println("enter second number");
 
        int n2=c.nextInt();
        System.out.println("value of n1= "+n1);
        System.out.println("value of n2= "+n2);
        swap(n1,n2);
 
	}
 
}

output:

1
2
3
4
5
6
7
8
enter a first number
10
enter second number
20
value of n1= 10
value of n2= 20
after Swapin value of n1= 20
after Swapin value of n2= 10