+ 2
How to split input string into char array in c++?
I want to make some kind of algorithm with this
9 Answers
+ 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
+ 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.
+ 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());
+ 1
The Dark Lord
Why use an algorithm?
+ 1
I want to sort the string into alphabetical order
+ 1
My question is how to make string into char array?
+ 1
here is an example
https://code.sololearn.com/ceV4y1yPLIIc/?ref=app
+ 1
Thank for the respons guys
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".