lundi 28 mars 2022

is there a way to upper case a char from a vector in c++?

I'm trying to write a code that generates a password from a string, if user wants a 8 character pass the code takes a string with 5 letters and adds randomly 3 more chars at the end and changes randomly to upper case a letter. For example:

Input: pedro

Output: pEdRo/*+ (Symbols should be generated randomly)

But I'm getting: EEpEdro-$!

Why I'm getting EE at the beginning of the vector? I have tried to use .erase but did not work, also I always got the same chars at the end (-$!) instead of random chars. Hope you can help me :)

#include <vector>
#include <string>
#include <numeric>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<char> mixed;
    int passlen;
    string input;
    string output;
    const char *specials[10] = {"/", "*", "-", "+", "%", "$", "!", "¿", "[", "}"};

    cout << "Ingrese longitud de caracteres" << endl;
    cin >> passlen;

    if (passlen == 8)
    {
        cout << "Ingrese cadena de 5 caracteres" << endl;
        cin >> input;
    }
    else
    {
        cout << "Ingrese cadena de 8 caracteres" << endl;
        cin >> input;
    }

    for (int i = 0; i <= input.length(); i++)
    {
        char toPush = input[i];
        mixed.push_back(toPush);
    }
    for (int j = 0; j <= 2; j++)
    {
        char x = *specials[(rand() % 10) - 1];
        mixed.push_back(x);
    }

    for (int k = 0; k <= 2; k++)
    {
        int element = rand() % (mixed.size() - 3);
        mixed[element] = putchar(toupper(mixed[element]));
        // mixed.erase(mixed.begin());
    }
    output = accumulate(begin(mixed), end(mixed), output);

    cout << output << endl;

    return 0;
}



Aucun commentaire:

Enregistrer un commentaire