Skip to main content
GameDev.net gamedev.net
🔒 Locked

String into characters.

Started by monp Nov 16, 2010 at 4:39 AM 12 replies 2.6k views
Original Post
monp
monp
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.
wicked357
wicked357
You could do something like this...

std::string name = "NAME";char letter[255];int size = name.size();for(int i = 0; i < size; i++){      letter = name;}


Hope that helps you out.

EDIT:: Also, check this link out about matrix encryption it is fun to mess around with, I did it for one of my classes when I was in college, fun project. http://aix1.uottawa.ca/~jkhoury/cryptography.htm
AngleWyrm
AngleWyrm
#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();}
--"I'm not at home right now, but" = lights on, but no ones home
SuperVGA
SuperVGA
char* input_chars = input_string.c_str();


And take it from there up to string length.
Also as in the previous responses, you can do without pointers.
phresnel
phresnel
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;}


Hmmm ...

std::string name =     "01234567890123456789012345678901234567890123456789"  "01234567890123456789012345678901234567890123456789"  "01234567890123456789012345678901234567890123456789"  "01234567890123456789012345678901234567890123456789"  "01234567890123456789012345678901234567890123456789"  "0123456";...



Quote:
Original post by SuperVGA
char* input_chars = input_string.c_str();

Nitpitck:

The signature of std::string::c_str() is const char* c_str ( ) const;, therefore you can't assign to char * but only to const char* [const].

You should also not try to change anything through your input_chars pointer.

Also note that calling any non-const member function of std::string may invalidate input_chars, as happens when input_string goes out of scope.

If you ensure that your char-array remains valid and untouched, use it. If you need to store all chars for longer, I'd go with:

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);}



Preferably wrap that loop into a function named "split".
rip-off
rip-off
Quote:

Preferably wrap that loop into a function named "split".


What about:
#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));}
SuperVGA
SuperVGA
Quote:
Original post by rip-off
What about:
*** Source Snippet Removed ***
Alright that one is good.
Ok there's no excuse for not remembering about std::c_str() const, but this would work also:
#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;}
rip-off
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.
std::vector<char> vector(string.begin(), string.end());

Quote:

but this would work also

Again, unnecessarily complicated.
phresnel
phresnel
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.


Never shall one judge from a first glimpse. Me failed.

SuperVGA
SuperVGA
Quote:
Original post by rip-off
Quote:

but this would work also

Again, unnecessarily complicated.


I didn't say it could match yours, I merely said it worked. Before that, my statement was "Alright that one is good." - referring to your solution.

It was my proper solution to contrast the
char* input_chars = input_string.c_str();
I did earlier, that's all. You win. :D
monp
monp
I am trying to take user input, turn the letters into ASCII and modify them and convert them back letters and put the string back together.

Right now I have it where it will put into ASCII and then I tested to see if I changed the number. I don't know where to go from here.

#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;}


Edit: I have worked more on it and this is what I have so far. It works currently.

#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;}


[Edited by - monp on November 16, 2010 3:12:54 PM]
rip-off
rip-off
You could do this ny modifying the std::string in place:
#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;}

[edit:]
To explain a little more, characters are already integral types. You may use mathematical operations on them directly. Also, try to use a loop to handle repeated code, rather than writing something like the following:
charbuf[1] += 1;charbuf[2] += 2;charbuf[3] += 3;charbuf[4] += 4;


[Edited by - rip-off on November 16, 2010 3:35:00 PM]
phresnel
phresnel
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;    }


As with wicked357 post, this reads "come, exploit me, become root and fuck the system".

Never ever use fixed size arrays when there is no protection that this size can be abused.

Quote:
    int s = input.size();    int charbuf[s];

And that is not valid C++.


For both there is a potion named std::vector<>.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.