How to reverse the words from the given string sentence?

   

 
  • How to reverse the words from the given string sentence?

String: MSK Technologies


Output: KSM seigolonhceT 





public class Demo1 {

public static void main(String[] args) {

String s="MSK Technologies";

String[] words = s.split(" ");

        int x,y;

        String reverseString = "";

         

        for ( x = 0; x < words.length; x++) 

        {

            String word = words[x];

             

            String reverseWord = "";

             

            for ( y = word.length()-1; y >= 0; y--) 

            {

                reverseWord = reverseWord + word.charAt(y);

            }

             

            reverseString = reverseString + reverseWord + " ";

        }

         

        System.out.println(s);

         

        System.out.println(reverseString);

         

        

    }

     


}


Output:

KSM seigolonhceT 


more programs visit http://sateeshm.com/

remove first and last occurrence of string java

   

 remove first and last occurrence of string java

String: codedecode
input: e

Output: coddecod


public class Demo200 {


public static void main(String[] args) {

String s="codedecode";

char ch='e';

for (int i = 0; i < s.length(); i++)

    {

 

       

        if (s.charAt(i) == ch)

        {

            s = s.substring(0, i) +

                s.substring(i + 1);

            break;

        }

    }

 

   

    for (int i = s.length() - 1; i > -1; i--)

    {

 

       

        if (s.charAt(i) == ch)

        {

            s = s.substring(0, i) +

                s.substring(i + 1);

            break;

        }

    }

  System.out.println(s);


}


}


Output:

coddecod


more programs visit http://sateeshm.com/

Count the unique characters in a given String

  

 Count the unique characters in a given String

String: Sateesh

Output: 3


public class Demo200 {


public static void main(String[] args) {

String s="sateesh";

StringBuffer str=new StringBuffer(s);

for (int i = 0; i < str.length(); i++)

    {

        int flag = 0;

        for (int j = 0; j < str.length(); j++)

        {

            

            if (str.charAt(i) == str.charAt(j) && i != j)

            {

                flag = 1;

               

                break;

            }

        }

        if (flag == 0)

            System.out.print(str.charAt(i));

    }

}


}


Output:

3


more programs visit http://sateeshm.com/

Display unique characters in a given String

 

 Display unique characters in a given String

String: Sateesh

Output: ath


public class Demo200 {


public static void main(String[] args) {

String s="sateesh";

StringBuffer str=new StringBuffer(s);

for (int i = 0; i < str.length(); i++)

    {

        int flag = 0;

        for (int j = 0; j < str.length(); j++)

        {

           

            if (str.charAt(i) == str.charAt(j) && i != j)

            {

                flag = 1;

               

                break;

            }

        }

        if (flag == 0)

            System.out.print(str.charAt(i));

    }

}


}


Output:

ath


more programs visit http://sateeshm.com/

Count the number of divisible numbers in a given range

 Count the number of divisible numbers in a given range

Range: 1-100
Number : 2

Output: 50

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,count=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%n==0)

{

count=count+1;

}

x++;

}while(x<=100);

System.out.println(count);

}



}



Output:

Enter n value

2

50


more programs visit http://sateeshm.com/

Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop

 

  • Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0 && n%2==0)

{

sum=sum+x;

}

else if(x%2!=0 && n%2!=0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println("sum is " +sum);

}



}



output:


Enter n value 5

sum is 9


Enter n value 6

sum is 12

Sum Of Even Numbers DoWhile Loop

  Sum Of Even Numbers DoWhile Loop 


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println(sum);

}



}


Output:

Enter n value

5

6


Sum Of Natural Numbers DoWhile Loop

 

  • Sum Of Natural Numbers DoWhile Loop

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

sum=sum+x;

x++;

}while(x<=n);

System.out.println(sum);

}



}



output:

Enter n value

5

15