Changing String Characters

Hey guys,

I am fairly new to C++ and I was wondering how I might go about converting strings such as:

Mushrooms, into

<Mu$hro0Mz!> or something very obscured by still resembled by the new characters.

I was thinking, in my uneducated position, of a table look up or a file, XML or something with a list of possible changes.

So if there was an S that needed to be replaced, the file would have: s, S, $ and 5 next to it and it would chose randomly which one to replace the S with.

Any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>


std::string replace(const char * input)
{
    std::string characters = "sS$5";
    std::string str = input;
    int len = str.length();

    str.insert(0, "<");
    str.append(">");

    for(int i = 0; i < len; i++)
    {
        size_t found;
        int x;

        found = str.find("s");
        if(found != std::string::npos)
        {
            srand ( time(NULL) );
            x = rand() % 4;
            std::string rs = characters.substr(x, 1);

            str.replace(found, 1, rs);
        }
    }
    std::cout << str << std::endl;
    return str;
}

int main()
{
    replace("Mushrooms");
    std::cin.get();

    return 0;
}



The code might not be very beautiful, but it might give you some ideas on how to approach these kind of problems.
Hmm, as I am a newb I haven't seen or know what :: do :S

I tried to add a method for input and I just got huge errors :S
What kind of method are you talking about? Use the replace function. If you want to let the user input the word, you can either ask with cin or use arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(int argc, char* argv[])
{
        if(argc < 2)
        {
                std::cout << "Usage: " << argv[0] << "[word]" << std::endl;
                std::cout << "Example: " << argv[0] << "Mushrooms" << std::endl;
                exit(1);
        }

        replace(argv[1]);

        std::cin.get();

        return 0;
}


It seems like I've done something wrong with the randomizing part, I'm not really sure though. It seems like the seed isn't initialized correctly, but I'll leave that to someone else.
I think a lookup table would be a pretty good way to go to start with, if you limit yourself to ascii chars.

1
2
3
4
5
6
7
8
9
10
// 256 entries : one per char
static const char* g_substs[] =
{ "a@"
, "b"
...
, "s$"
...
, "z" };

static const size_t g_substCount = sizeof(g_substs)/sizeof(g_substs[0]);


Then you "substChar()" function would:
- lookup entry for char, using it as the index
- get length of substr string and randomly choose one char in it
- randomly change case (leave alone/toupper/tolower)

You could convert the lookup table to a std::vector which you populate from a file (XML or otherwise) later on.

To support Unicode it would probably be better with a std::map, as you'll only want to handle a limited number of the available wchars_t values. Or you could use the first char in the lookup table as a key and search for it, rather than using the char directly as an index.
Last edited on
Hey, im new here. could you please tell mo how to you write code snippets here in the forum? thanks a lot!
You mean the tagged text?

"How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.