-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Wednesday, June 10, 2020

Java Program To Convert String To Char

  • June 10, 2020
  • by

Java Program To Convert String To Char

In this Java Program, we convert String to Char by using charAt() method

Logic :  char ch = str.charAt(i);

Step 1 : Create variable str and store the string "Hello" in it.

Step 2 : Use for loop by initializing i = 0; i < str.length(); i++

Step 3 : Create variable ch and store the result of the charAt() method

Step 4 : Display the result by using System.out.print method

Program :
               
public class StringToChar 
    {
        public static void main(String[] args)
        {
            //Using charAt() method
            String str = "Hello";
            for(int i = 0; i <str.length(); i++)
            {
                char ch = str.charAt(i);
                System.out.println("Character at "+i+"Position:"+ch);
           
            } 
              
        }
    
}

Output:

Character at 0 Position: H
Character at 1 Position: e
Character at 2 Position: l
Character at 3 Position: l
Character at 4 Position: o


For the execution of the same do visit my YouTube Channel:

A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages