
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Iterate through each character of the string.
In this article, we will understand how to iterate through each character of the string. String is a datatype that contains one or more characters and is enclosed in double quotes (“ ”). Char is a datatype that contains an alphabet or an integer or a special character.
Below is a demonstration of the same −
Suppose our input is −
The string is defined as: Java Program
The desired output would be −
The characters in the string are: J, a, v, a, , P, r, o, g, r, a, m,
Algorithm
Step 1 - START Step 2 - Declare a string namely input_string, a char namely temp. Step 3 - Define the values. Step 4 - Iterate over the string, print each character at index ‘i’ of the string along with a blank space. Step 5 - Display the result Step 6 - Stop
Example 1
Here, for-loop.
public class Characters { public static void main(String[] args) { String input_string = "Java Program"; System.out.println("The string is defined as: " +input_string); System.out.println("The characters in the string are: "); for(int i = 0; i<input_string.length(); i++) { char temp = input_string.charAt(i); System.out.print(temp + ", "); } } }
Output
The string is defined as: Java Program The characters in the string are: J, a, v, a, , P, r, o, g, r, a, m,
Example 2
Here, for-each loop.
public class Main { public static void main(String[] args) { String input_string = "Java Program"; System.out.println("The string is defined as: " +input_string); System.out.println("The characters in the string are: "); for(char temp : input_string.toCharArray()) { System.out.print(temp + ", "); } } }
Output
The string is defined as: Java Program The characters in the string are: J, a, v, a, , P, r, o, g, r, a, m,
- Related Articles
- Golang program to iterate through each character of string
- Java Program to Iterate through Elements of HashMap
- Java program to check occurence of each character in String
- Java program to check occurrence of each character in String
- Java Program to Capitalize the first character of each word in a String
- Java program to count the occurrence of each character in a string using Hashmap
- Java program to count the occurrences of each character
- How to iterate through Java List?
- Iterate through elements of Java LinkedHashSet
- Iterate through the values of Java LinkedHashMap in Java
- Golang Program to Iterate through Elements of Dictionary
- Iterate through the values of HashMap in Java
- Iterate through the values of TreeMap in Java
- Java Program to access character of a string
- Iterate through Java Unit Tuple

Advertisements