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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago