Open In App

3 Ways for Traversal of Strings

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 s

Input: str = “Coder”
Output: C o d e r



Method 1: Using For Loops

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

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)

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
!






Article Tags :