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
forloop 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++
#include <iostream>
#include <string>
int main() {
std::string text = "Hello,World!";
for (char c : text) {
std::cout << c << std::endl;
}
return 0;
}
// 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
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));
}
}
}
# Python
text = "Hello, World!"
for char in text:
print(char)
// 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
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
whileloop 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++
#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
#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
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++;
}
}
}
# Python
text = "Hello, World!"
index = 0
while index < len(text):
print(text[index])
index += 1
// 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
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
forEachmethod 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 (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 !