迭代字符串中字符的Java程序
给定长度为N的字符串str ,任务是遍历字符串并使用Java打印给定字符串的所有字符。
插图:
Input : str = “GeeksforGeeks”
Output : G e e k s f o r G e e k s
Input : str = "GfG"
Output : G f G
方法:
- 使用 for 循环(天真的方法)
- 使用迭代器(最佳方法)
方法一:使用for循环
解决这个问题的最简单或者更确切地说我们可以说是天真的方法是通过使用变量 ' i'使用for 循环进行迭代,直到字符串的长度,然后打印字符串中存在的每个字符的值。
例子
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 = "GeeksforGeeks";
// Calling the getChar method
getChar(str);
}
}
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);
}
}
输出
G e e k s f o r G e e k s
Time complexity is O(N) and space complexity is O(1)
方法二:使用迭代器
可以使用迭代器遍历字符串。我们将从Java.text 包中导入CharacterIterator和StringCharacterIterator类
例子:
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);
}
}
输出
G f G
Time Complexity: O(N) and space complexity is of order O(1)