String Traversal operations refers to process of one character at a time. Often we start at the beginning, select each character in turn, do something to it, and continue until the end.
Examples:
Input: str = “GeeksforGeeks”
Output: G e e k s f o r G e e k sInput: str = “Coder”
Output: C o d e r
Method 1: Using For Loops
- Initialize a variable with the string you want to traverse.
-
Use a
for
loop to iterate over each character in the string. - In each iteration, the loop variable holds the current character.
- You can print or process the character within the loop.
For Example:
C++
// C++ #include <iostream> #include <string> int main() {
std::string text = "Hello,World!";
for ( char c : text) {
std::cout << c << std::endl;
}
return 0;
} |
C
// C #include <stdio.h> #include <string.h> int main() {
char text[] = "Hello, World!";
int len = strlen (text);
for ( int i = 0; i < len; i++) {
printf ("%c\n", text[i]);
}
return 0;
} |
Java
// Java public class StringTraversal {
public static void main(String[] args) {
String text = "Hello, World!";
for ( int i = 0 ; i < text.length(); i++) {
System.out.println(text.charAt(i));
}
}
} |
Python3
# Python text = "Hello, World!"
for char in text:
print (char)
|
C#
// C# using System;
public class StringTraversal
{ public static void Main( string [] args)
{
string text = "Hello, World!" ;
for ( int i = 0; i < text.Length; i++)
{
Console.WriteLine(text[i]);
}
}
} // This code is contributed by Tapesh(tapeshdua420) |
Javascript
// JavaScript const text = "Hello, World!"; for (let i = 0; i < text.length; i++) {
console.log(text[i]);
} |
Output
H e l l o , W o r l d !
Method 2: Using a While Loop
- Initialize a variable with the string you want to traverse.
-
Initialize an index variable (e.g.,
index
) to0
. -
Use a
while
loop that continues as long as the index is less than the length of the string. - Within the loop, access the character at the current index and process it.
- Increment the index after processing the character to move to the next character.
For Example:
C++
// C++ #include <iostream> #include <string> int main() {
std::string text = "Hello, World!";
int index = 0;
while (index < text.length()) {
std::cout << text[index] << std::endl;
index++;
}
return 0;
} |
C
// C #include <stdio.h> #include <string.h> int main() {
char text[] = "Hello, World!";
int len = strlen (text);
int index = 0;
while (index < len) {
printf ("%c\n", text[index]);
index++;
}
return 0;
} |
Java
// Java public class StringTraversal {
public static void main(String[] args) {
String text = "Hello, World!";
int index = 0 ;
while (index < text.length()) {
System.out.println(text.charAt(index));
index++;
}
}
} |
Python3
# Python text = "Hello, World!"
index = 0
while index < len (text):
print (text[index])
index + = 1
|
C#
// C# Implementation using System;
public class StringTraversal
{ public static void Main( string [] args)
{
string text = "Hello, World!" ;
int index = 0;
while (index < text.Length)
{
Console.WriteLine(text[index]);
index++;
}
}
} // This code is contributed by Tapesh(tapeshdua420) |
Javascript
// JavaScript const text = "Hello, World!"; let index = 0; while (index < text.length) {
console.log(text[index]);
index++;
} |
Output
H e l l o , W o r l d !
Method 3: Using forEach (JavaScript Specific for Arrays)
- This method is specific to JavaScript and is used when you have an array of characters created from a string.
- Create an array from the string, where each element of the array represents a character.
-
Use the
forEach
method on the array to iterate over each character. - In each iteration, the loop function receives the current character as an argument.
- You can print or process the character within the loop.
For Example:
Javascript
// JavaScript (for an array of characters) const text = "Hello, World!"; const chars = text.split( '' );
chars.forEach( function (char) {
console.log(char);
}); |
Output
H e l l o , W o r l d !
Recommended Articles
3. Pre Order, Post Order and In Order traversal of a Binary Tree in one traversal | (Using recursion)
4. Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree