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 = “GeeksforGeeks”
Output: G e e k s f o r G e e k sInput: 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 = "GeeksforGeeks"; // Stores length of the string int N = str.length(); TraverseString(str, N); } |
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 = "GeeksforGeeks"; // Stores length of the string int N = str.length(); TraverseString(str, N); } |
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 = "GeeksforGeeks"; // Stores length of the string int N = str.length(); TraverseString(str, N); } |
G e e k s f o r G e e k s
Time Complexity: O(N)
Auxiliary Space: O(1)
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to Iterate through a String word by word in C++
- How to iterate through a Vector without using Iterators in C++
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Strings formed from given characters without any consecutive repeating characters
- Longest substring with atmost K characters from the given set of characters
- Minimum characters to be replaced to make frequency of all characters same
- Maximum non-repeating characters after removing K characters
- Enum Classes in C++ and Their Advantage over Enum DataType
- Find the smallest window in a string containing all characters of another string
- String matching where one string contains wildcard characters
- Create a new string by alternately combining the characters of two halves of the string in reverse
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

