Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaJava Program to Iterate Over Characters in String

Java Program to Iterate Over Characters in String

Given string str of length N, the task is to traverse the string and print all the characters of the given string using java.

Illustration:

Input  : str = “Lazyroar”
Output : G e e k s f o r G e e k s
Input  : str = "GfG"
Output : G f G

Methods:

  1. Using for loops(Naive approach)
  2. Using iterators (Optimal approach)

Method 1: Using for loops

The simplest or rather we can say naive approach to solve this problem is to iterate using a for loop by using the variable ‘i’ till the length of the string and then print the value of each character that is present in the string. 

Example

Java




// Java Program to Iterate Over Characters in String
  
// Class 1
// Main class
// To iterate over characters
class GFG {
  
    // Method 1
    // To traverse the string and
    // print the characters of the string
    static void getChar(String str)
    {
  
        // Traverse the string using for loop
        for (int i = 0; i < str.length(); i++) {
  
            // Printing the current character
            System.out.print(str.charAt(i));
  
            // Printing a space after each letter
            System.out.print(" ");
        }
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating a String variable to store the string
        String str = "Lazyroar";
  
        // Calling the getChar method
        getChar(str);
    }
}


Output

G e e k s f o r G e e k s 

Time complexity is O(N) and space complexity is O(1)

Method 2: Using iterators

The string can be traversed using an iterator. We would be importing CharacterIterator and StringCharacterIterator classes from java.text package

Example:

Java




// Java Program to Iterate Over Characters in String
  
// Importing input output classes
import java.io.*;
// Importing CharacterIterator and StringCharacterIterator
// classes from java.text package
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
  
// Main class
// To iterate over characters
class GFG {
  
    // Method 1
    // To traverse the string and
    // print the characters of the string
    static void getChar(String str)
    {
  
        // Creating a CharacterIterator variable
        CharacterIterator itr
            = new StringCharacterIterator(str);
  
        // Iterating using while loop
        while (itr.current() != CharacterIterator.DONE) {
  
            // Print the current character
            System.out.print(itr.current());
  
            // Print a space after each letter
            System.out.print(" ");
  
            // Getting the next input from the user
            // using the next() method
            itr.next();
        }
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a String variable to store the string
        String str = "GfG";
  
        // Calling the getChar method
        getChar(str);
    }
}


Output

G f G 

Time Complexity: O(N) and space complexity is of order O(1)

RELATED ARTICLES

Most Popular

Recent Comments