C++'ta string nasıl karakterlere ayrılır?

GT 145

Decapat
Katılım
29 Temmuz 2021
Mesajlar
723
Çözümler
1
Arkadaşlar C++ ile input aldıktan sonra bunu mesela "selam" ise 's','e' vb. diye parçalayıp bir arraya aktarmak istiyorum. Nasıl yaparım?
 
Son düzenleyen: Moderatör:
subscript operatörünü kullanabilirsin msg[0], yapmak istediğin c stili bir diziye aktarmak ise zaten string bunun için bir fonksiyon sağlıyor msg.c_str(); Başka bir diziye kopyalamak için strcpy kullanabilirsin vs.
 
C++:
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    string msg = "test message";
    
    for(auto chr: msg) {
        cout << chr << endl;
    }
    
    char arr[25];
    std::strcpy(arr, msg.c_str());
    cout << "arr: " << arr << endl;
    
    
    for(int i=0; i< msg.size(); ++i) {
        cout << msg[i] << endl;
    }
    return 0;
}
 

Yeni konular

Yeni mesajlar

Geri
Yukarı