Original Post
How would I go about taking a user's input (string) and split it up into individual characters. I am trying mess with encrypting a simple string.
std::string name = "NAME";char letter[255];int size = name.size();for(int i = 0; i < size; i++){ letter = name;}#include <iostream>#include <string>using namespace std;int main(){ string input; cout << "provide input: "; getline(cin, input); cout << "length of [" << input.c_str() << "] is " << input.length();}char* input_chars = input_string.c_str();Quote:
Original post by wicked357 std::string name = "NAME";char letter[255];int size = name.size();for(int i = 0; i < size; i++){ letter = name;}
std::string name = "01234567890123456789012345678901234567890123456789" "01234567890123456789012345678901234567890123456789" "01234567890123456789012345678901234567890123456789" "01234567890123456789012345678901234567890123456789" "01234567890123456789012345678901234567890123456789" "0123456";...Quote:
Original post by SuperVGA char* input_chars = input_string.c_str();
std::string foobar = ...;std::vector<char> chars;typedef std::string::const_iterator iterator;for (iterator it=foobar.begin(), end=foobar.end(); it != end; ++it){ chars.push_back (*it);}Quote:
Preferably wrap that loop into a function named "split".
#include <string>#include <vector>#include <iterator>#include <iostream>int main(){ std::string string = "whatever, trevor"; std::vector<char> vector(string.begin(), string.end()); std::copy(vector.begin(), vector.end(), std::ostream_iterator<char>(std::cout));}Quote:Alright that one is good.
Original post by rip-off
What about:
*** Source Snippet Removed ***
#include <sstream>#include <vector>int main(){ std::string input_string = "NAME"; std::vector<char> input_chars(input_string.length()); for(unsigned i = 0; input_string.length() != i; ++i) { input_chars = input_string.c_str(); } return 0;}Quote:
I already looked into algorithms, but it appeared that there was no split function. But std::copy is perfect :)
std::vector<char> vector(string.begin(), string.end());Quote:
but this would work also
Quote:
Original post by rip-off Quote:
I already looked into algorithms, but it appeared that there was no split function. But std::copy is perfect :)
The std::copy line is just to show the vector contains the correct data. The line to convert the string into a vector is a mere one-liner.
Quote:
Original post by rip-off Quote:
but this would work also
Again, unnecessarily complicated.
char* input_chars = input_string.c_str(); I did earlier, that's all. You win. :D
#include <iostream>#include <string>int main(){ std::string input; char a[255]; std::cin >> input; int s = input.size(); int charbuf[s]; for(int i = 0; i < s; i++) { a = input; } std::cout << input.size() << std::endl; charbuf[0] = (int) a[0]; charbuf[1] = (int) a[1]; charbuf[2] = (int) a[2]; charbuf[3] = (int) a[3]; charbuf[4] = (int) a[4]; std::cout << charbuf[1] << "unmodified" << std::endl; charbuf[1] += 5; std::cout << charbuf[1] << "Modified" << std::endl; return 0;}#include <iostream>#include <string>int main(){ std::string input; char a[255]; std::cin >> input; int s = input.size(); int charbuf[s]; char cback[s]; std::string s1, s2, s3, s4, s5; std::string result; for(int i = 0; i < s; i++) { a = input; } std::cout << input.size() << std::endl; charbuf[0] = (int) a[0]; charbuf[1] = (int) a[1]; charbuf[2] = (int) a[2]; charbuf[3] = (int) a[3]; charbuf[4] = (int) a[4]; charbuf[1] += 1; charbuf[2] += 2; charbuf[3] += 3; charbuf[4] += 4; cback[0] = (char) charbuf[0]; cback[1] = (char) charbuf[1]; cback[2] = (char) charbuf[2]; cback[3] = (char) charbuf[3]; cback[4] = (char) charbuf[4]; s1 = cback[0]; s2 = cback[1]; s3 = cback[2]; s4 = cback[3]; s5 = cback[4]; result = s1 + s2 +s3 + s4 + s5; std::cout << result << std::endl; return 0;}#include <iostream>#include <string>int main(){ std::string text; std::cin >> text; for(std::string::size_type i = 0 ; i < text.size() ; ++i) { text += i; } std::cout << text<< std::endl; return 0;}charbuf[1] += 1;charbuf[2] += 2;charbuf[3] += 3;charbuf[4] += 4;Quote:
Original post by monp std::string input; char a[255]; std::cin >> input; int s = input.size(); [...] for(int i = 0; i < s; i++) { a = input; }
Quote:
int s = input.size(); int charbuf[s];
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more