String

Program to print Characters of a string in Java

Program to print Characters of a string in Java

In this tutorial, we will discuss the concept of Program to print Characters of a string in Java

In this topic, we are going to learn how to display characters of a string in Java programming language  using for, while and do-while loops.

Code to  display characters of a string in Java

characters

Code to display characters of a string using for loop

In this program, we are briefing how to display characters of a string using for loop in Java language

Program 1

import java.util.Scanner;
public class PrintStringCharfor{
public static void main(String args[]){
String str;
    int i;
    Scanner sc=new Scanner(System.in);
    
    System.out.println("Please enter the String: ");
str=sc.nextLine();
for(i=0; i<str.length(); i++){
     System.out.println("The charater of posotion "+i+": "+str.charAt(i));
}
}
}

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

Please enter the string:
Java
The character of position 0 : J
The character of position 1 : a
The character of position 2 : v
The character of position 3 : a

 

Code to display characters of a string using while loop

In this program, we are briefing how to display characters of a string using while loop in Java language

Program 2

import java.util.Scanner;
public class PrintStringCharwhile{
public static void main(String args[]){
String str;
    int i;
    Scanner sc=new Scanner(System.in);
    
    System.out.println("Please enter the String: ");
str=sc.nextLine();
i=0;
while( i<str.length()){
     System.out.println("The charaters of position: "+i+": "+str.charAt(i));
         i++;
}
}
}

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

Please enter the string:
Language
The character of position 0 : L
The character of position 1 : a
The character of position 2 : n
The character of position 3 : g
The character of position 4 : u
The character of position 5 : a
The character of position 6 : g
The character of position 7 : e

 

 

Code to display characters of a string using do-while loop

In this program, we are briefing how to display characters of a string using do-while loop in Java language

Program 3

import java.util.Scanner;
public class PrintStringCharDowhile{
public static void main(String args[]){
String str;
    int i;
    Scanner sc=new Scanner(System.in);
    
    System.out.println("Please enter the String: ");
str=sc.nextLine();
i=0;
do{
     System.out.println("The charaters of position "+i+": "+str.charAt(i));
         i++;
}while(i<str.length());
}
}

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

Please enter the string:

DoWhile
The character of position 0 : D
The character of position 1 : o
The character of position 2 : W
The character of position 3 : h
The character of position 4 : i
The character of position 5 : l
The character of position 6 : e

 

Similar post

Java program to print integer in Java language

Java program to print string array

Java program to read and print string array

C program to print string array

C program to read and print string array

C++ program print string array

C++ program to read print string array

 

 

Suggested post

for loop in Java language

while loop in Java language

Do-while loop in Java language

Enhanced for loop in Java language

Data  type and variable in Java language

Operator in Java language

 

Java program to fill an array of characters from user input
C++ Program to print Characters in a string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago