Str to char
Table of Contents
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
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
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
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
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
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
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
Do-while loop in Java language
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.