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.
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
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
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…