Iterate Through Character Of String in C++

Are you looking for different ways you can use to iterate through the string in C++? In this article, I will show you how you can iterate over the character of string in c++ easily.

Using For Loop (Simple Approach)

The first and most basic method you can use is by using the For Loop on your string to iterate through characters of it. Let us see in example code below.

#include <iostream>
#include <string>

int main ()
{
  //creating a string with some value
  std::string str  = "Coduber.com";

  //initializing the for loop 
  for(int i=0;i<str.size();i++){
    //printing each character one by one
    std::cout<<str[i];
  }

  return 0;
}

Output:

Coduber.com

Using Iterator of String Class

You can use the iterator present in the string class in C++. It is initialized as std::string::iterator. It returns a constant iterator pointing to the character of the string.

#include <iostream>
#include <string>

int main ()
{
  //creating a string with some value
  std::string str  = "Using String Iterator Coduber.com";

  //initializing std::string::iterator
  for(std::string::iterator it = str.begin(); it!=str.end(); ++it){
    //printing each character one by one
    std::cout<<*it;
  }

  return 0;
}

Output:

Using String Iterator Coduber.com

Using Explicit Auto

You can use an explicit auto iterator on a string to print all the characters of the string. It is a generalized approach and is the most commonly used. Also, it is considered as one of the best practices.

#include <iostream>
#include <string>

int main ()
{
  //creating a string with some value
  std::string str  = "Coduber.com";

  //initializing auto iterator explicitly
  for(auto it = str.begin(); it!=str.end(); ++it){
    //printing each character one by one
    std::cout<<*it;
  }

  return 0;
}

Output:

Coduber.com

Using Range Based Loop for C++ 11 and Above

If you are using C++ 11 and above then you can use the range-based for loop to print each character of this string. This method or for loop is very similar to for loop you will find in Python, JAVA, and other advanced languages.

#include <iostream>
#include <string>

int main ()
{
  //creating a string with some value
  std::string str  = "Coduber.com";

  //initializing Range based for loop
  for(char s:str){
    //printing each character one by one
    std::cout<<s<<" ";
  }

  return 0;
}

Output:

C o d u b e r . c o m

Using for_each STL Method to Iterate Over String

for_each is the STL method present in c++. Using this method you can pass each character of the string one by one to a given input function through which you can print all those characters.

#include <iostream>
#include <string>
#include <algorithm>

void printChar(char &s){
  std::cout<<s<<" ";
}

int main ()
{
  //creating a string with some value
  std::string str  = "Coduber.com";

  //initializing for_each STL
  std::for_each(str.begin(), str.end(), printChar);

  return 0;
}

Output:

Iterate Through Character Of String in C++

Using c_str() to print each character

You can copy the same string in an array of characters and then use a for loop to print the character. This method is only useful when you want to print any specific character. This method is not recommended.

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

int main ()
{
  //creating a string with some value
  std::string str  = "Coduber.com";

    //creating a char array with size of the string plus one
  const char* convertString = str.c_str();
  int size = str.size();
  for(int i=0; i<size; i++){
    std::cout<<convertString[i]<<" ";
  }


  return 0;
}

If you liked our post related to how to iterate through the character of string in C++ then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Also, subscribe to our newsletter to get notifications for the new posts.

Further Read:

  1. 2D Vector Implementation in C++ Complete Guide
Share your love

One comment

Leave a Reply