📜  在C++中遍历字符串的字符

📅  最后修改于: 2021-05-30 12:12:18             🧑  作者: Mango

给定长度为N的字符串str ,任务是遍历字符串并打印给定字符串的所有字符。

例子:

天真的方法:解决此问题的最简单方法是使用变量i迭代[0,N – 1]范围内的循环,其中N表示字符串的长度,并打印str [i]的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
  
#include 
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);
}


C++
// C++ program to implement
// the above approach
  
#include 
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);
}


C++
// C++ program to implement
// the above approach
  
#include 
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

时间复杂度: O(N)
辅助空间: O(1)

基于自动关键字的方法:可以使用自动迭代器遍历字符串。

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
  
#include 
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

时间复杂度: O(N)
辅助空间: O(1)

基于迭代器的方法:可以使用迭代器遍历字符串。

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
  
#include 
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

时间复杂度: O(N)
辅助空间: O(1)

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”