+ 2

How to split input string into char array in c++?

I want to make some kind of algorithm with this

12th Sep 2018, 3:12 AM
Fajar Maftuh Fadli
Fajar Maftuh Fadli - avatar
9 Réponses
+ 7
#include <iostream> #include <string> using namespace std; int main() { string str = "Hello World"; cout << str << endl; for(int i = 0; i < str.size(); i++) { for(int j = 0; j < (str.size() - 1); j++) { if(str[j] > str[j+1]) { char temp = str[j]; str[j] = str[j+1]; str[j+1] = temp; } } } cout << str << endl; return 0; } [Edit]: The Dark Lord This is for sorting characters in a string. You don't need to convert the string to char array to sort
12th Sep 2018, 7:17 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
The Dark Lord That would need an algorithm. A combo of a - string to char array - possibly a to lower function - then a sorting array that sorts the chars in other.
12th Sep 2018, 3:50 AM
Manual
Manual - avatar
+ 2
string mystring = "hello"; char* mychararray = mystring.c_str(); But you probably don't need that. A string can do all the array-like things you need. It has a .size() and you can use mystring[i] to get the i-th character. To sort a string, you can std::sort(mystring.start(), mystring.end());
12th Sep 2018, 4:12 AM
Schindlabua
Schindlabua - avatar
+ 1
The Dark Lord Why use an algorithm?
12th Sep 2018, 3:38 AM
Manual
Manual - avatar
+ 1
I want to sort the string into alphabetical order
12th Sep 2018, 3:40 AM
Fajar Maftuh Fadli
Fajar Maftuh Fadli - avatar
+ 1
My question is how to make string into char array?
12th Sep 2018, 3:53 AM
Fajar Maftuh Fadli
Fajar Maftuh Fadli - avatar
12th Sep 2018, 3:54 AM
Manual
Manual - avatar
+ 1
Thank for the respons guys
12th Sep 2018, 5:10 AM
Fajar Maftuh Fadli
Fajar Maftuh Fadli - avatar
0
you can use the extension method for turning a string into a char*: string a = "abcdefg"; char* b = a.c_str(); now "b" is a char array with the contents of "a".
12th Sep 2018, 9:12 AM
hinanawi
hinanawi - avatar