Saturday, August 30, 2025
HomeData Modelling & AIIterate over characters of a string in C++

Iterate over characters of a string in C++

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

Examples:

Input: str = “neveropen”
Output: G e e k s f o r G e e k s

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

Naive Approach: The simplest approach to solve this problem is to iterate a loop over the range [0, N – 1], where N denotes the length of the string, using variable i and print the value of str[i].

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to traverse the string and
// print the characters of the string
void TraverseString(string &str, int N)
    // Traverse the string
    for (int i = 0; i < N; i++) {
  
        // Print current character
        cout<< str[i]<< " ";
    }
      
}
  
// Driver Code
int main()
{
    string str = "neveropen";
      
    // Stores length of the string
    int N = str.length();
  
    TraverseString(str, N);
}


Output:

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

Time Complexity: O(N)
Auxiliary Space: O(1)

Auto keyword – based Approach: The string can be traversed using auto iterator.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to traverse the string and
// print the elements of the string
void TraverseString(string &str, int N)
{
    // Traverse the string
    for (auto &ch : str) {
  
        // Print current character
        cout<< ch<< " ";
    }
}
// Driver Code
int main()
{
    string str = "neveropen";
  
    // Stores length of the string
    int N = str.length();
  
    TraverseString(str, N);
}


Output:

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

Time Complexity: O(N)
Auxiliary Space: O(1)

Iterator – based Approach: The string can be traversed using iterator.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to traverse the string and
// print the elements of the string
void TraverseString(string &str, int N)
{
      
    // Stores address of 
    // a character of str
    string:: iterator it;
      
    // Traverse the string
    for (it = str.begin(); it != str.end();
                                   it++) {
        // Print current character
        cout<< *it<< " ";
    }
}
  
// Driver Code
int main()
{
    string str = "neveropen";
      
      
    // Stores length of the string
    int N = str.length();
    TraverseString(str, N);
}


Output:

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

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

6 COMMENTS

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6733 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS