Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to print characters in a string using Java

Program to print characters in a string using Java

Posted on November 24, 2021

Table of Contents

  • Program to print characters in a string using Java
    • Code to separate  characters of a string
      • Code to separate characters of a string using for loop – #1
      • Code to separate characters of a string using for loop – #2
      • Code to separate characters of a string using while loop – #1
      • Code to separate characters of a string using while loop – #2
      • Code to separate characters of a string using do-while loop – #1
      • Code to separate characters of a string using do-while loop – #2

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes