For ststement

Program to print characters in a string using Java

Program to print characters in a string using Java

In this article, we will discuss the concept of Program to print characters in a string using Java

In this post, we are going to learn how to write a program to  separate characters of a given string  in Java language

Code to separate  characters of a string

Code to separate characters of a string using for loop – #1

In this code, we are going to learn how to separate individual characters in the given string   using for loop in Java language

Program 1

public class PrintStringCharfor1{
  //class with class name
public static void main(String args[]){
  //main method
String str="String";//declare a string variale
  int i;//declare a integer variable
for(i=0; i<str.length(); i++){
     System.out.println("The charaters of posotion: "+i+": "+str.charAt(i));
    //display the characters of a string
}
}
}

When the above code is executed, it produces the following result

The characters of a position 0: S
The characters of a position 1: t
The characters of a position 2: r
The characters of a position 3: i
The characters of a position 4: n
The characters of a position 5: g

 

Code to separate characters of a string using for loop – #2

In this code, we are going to learn how to separate individual characters in the given string   using for loop in Java language

Program 2

import java.util.Scanner;
public class PrintStringCharfor{
  //class with class name
public static void main(String args[]){
  //main method
String str;//declare a string variale
  int i;//declare a integer variable
  Scanner sc=new Scanner(System.in);
  //scanner object for user input
  System.out.println("Please enter your String: ");
str=sc.nextLine();
for(i=0; i<str.length(); i++){
     System.out.println("The charaters of position: "+i+": "+str.charAt(i));
    //display the characters of a string
}
}
}

When the above code is executed, it produces the following result

Please enter your string
MyCode
The characters of a position 0: M
The characters of a position 1: y
The characters of a position 2: C
The characters of a position 3: o
The characters of a position 4: d
The characters of a position 5: e

 

Code to separate characters of a string using while loop – #1

In this code, we are going to learn how to separate individual characters in the given string   using while loop in Java language

Program 1

public class PrintStringCharwhile1{
  //class with class name
public static void main(String args[]){
  //main method
String str="Character";//declare a string variale
  int i;//declare a integer variable
  i=0;
while(i<str.length()){
     System.out.println("The charaters of posotion: "+i+": "+str.charAt(i));
    //display the characters of a string
     i++;
}
}
}

When the above code is executed, it produces the following result

The characters of a position 0: C
The characters of a position 1: h
The characters of a position 2: a
The characters of a position 3: r
The characters of a position 4: a
The characters of a position 5: c
The characters of a position 6: t
The characters of a position 7: e
The characters of a position 8: r

 

Code to separate characters of a string using while loop – #2

In this code, we are going to learn how to separate individual characters in the given string   using while loop in Java language

Program 2

import java.util.Scanner;
public class PrintStringCharwhile{
public static void main(String args[]){
String str;//declare a string variable
  int i;//declare a integer variable
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Please enter the String: ");
str=sc.nextLine();//Take input from the user
i=0;
while( i<str.length()){
     System.out.println("The charaters of index: "+i+": "+str.charAt(i));
     i++;//display character of a string
}
}
}

When the above code is executed, it produces the following result

Please enter a string
CodeJava

The characters of a position 0: C
The characters of a position 1: o
The characters of a position 2: d
The characters of a position 3: e
The characters of a position 4: J
The characters of a position 5: a
The characters of a position 6: v
The characters of a position 7: a

 

Code to separate characters of a string using do-while loop – #1

In this code, we are going to learn how to separate individual characters in the given string   using do-while loop in Java language

Program 1

public class PrintStringChardowhile1{
  //class with class name
public static void main(String args[]){
  //main method
String str="Variable";//declare a string variale
  int i;//declare a integer variable
  i=0;
do{
     System.out.println("The charaters of posotion: "+i+": "+str.charAt(i));
    //display the characters of a string
     i++;
}while(i<str.length());
}
}

When the above code is executed, it produces the following result

The characters of a position 0: V
The characters of a position 1: a
The characters of a position 2: r
The characters of a position 3: i
The characters of a position 4: a
The characters of a position 5: b
The characters of a position 6: l
The characters of a position 7: e


Code to separate characters of a string using do-while loop – #2

In this code, we are going to learn how to separate individual characters in the given string   using do-while loop in Java language

Program 2

import java.util.Scanner;
public class PrintStringCharDowhile{
public static void main(String args[]){
String str;//Declare string variable
  int i;//Declare character variable
  Scanner sc=new Scanner(System.in);
  //Scanner object for get user input
  System.out.println("Please enter the String: ");
str=sc.nextLine();//Take input from user
i=0;
do{
     System.out.println("The charaters of index: "+i+": "+str.charAt(i));
     i++;//Display characters of a string
}while(i<str.length());
}
}

When the above code is executed, it produces the following result

Please enter a string
MyStr
The characters of a position 0: M
The characters of a position 1: y
The characters of a position 2: S
The characters of a position 3: t
The characters of a position 4: r

 

Suggested for you

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

One dimensional array in Java

 

Similar post

Java program to read and print array elements using for loop

Java program to read and print array elements using while loop

Java program to read and print array elements using Do-while loop

C code to take and print array elements using for loop

C code to take and print array elements using while loop

C code to take and print array elements using do-while loop

C++ program to read and print array elements using for loop

C++ program to read and print array elements using while loop

C++ program to read and print array elements using do-while loop

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.