Store text variable in a char array

1

Good I am doing a problem that they left me in the university which asks me to develop a program which must store in a variable the text: "Pablito nailed a clavito. What clavito nailed Pablito? "As arrangement char. I think that it is already, then I ask that using pointers go through the previous variable and generate a new string where any random letter is replaced, that pointers still do not master it well and what appears on the internet is more with arrays and pointers. Whole type, if you know of any page or video that can instruct me I would appreciate this is what I have code just because I have no idea how to do the rest.

#include <iostream>

using namespace std;

int main()
{
    char palabra[] = {"Pablito clav2 un clavito. ¿Que clavito clav2   Pablito?"};
    char *puntero = palabra;

    cout << puntero << endl;

    return 0;
}
    
asked by Jose Vargas 12.11.2016 в 06:10
source

1 answer

3
  

using pointers, go through the previous variable and generate a new string where any random letter is replaced

You do not indicate the replacement criteria, so I will make a replacement according to my own criteria, which are the following:

  • 1/4 part of the letters could be replaced.
  • Replacement will be by letters between a and z .
  • Any character (letters, spaces, punctuations, symbols) is a candidate to be replaced.

On the other hand, what you call string is an array of static-sized characters ending with a null character ( 'copia' ), but for convenience we'll call it a string.

char palabra[] = {"Pablito clav2 un clavito. ¿Que clavito clav2   Pablito?"};
char copia[sizeof(palabra)] = {};

std::random_device rd_reemplazar;
std::mt19937 generador_reemplazar(rd_reemplazar());
std::bernoulli_distribution reemplazar(0.25); // 25% de posibilidad de reemplazar

std::random_device rd_letra;
std::mt19937 generador_letras(rd_letra());
std::uniform_int_distribution<char> letra('a', 'z');

for (char *origen = palabra, *destino = copia, *final = palabra + sizeof(palabra); origen != final; ++origen, ++destino)
    *destino = reemplazar(generador_reemplazar) ? letra(generador_letras) : *origen;

In the version of your code you have no place in which to generate a new chain, for this we have created an array called palabra that will be the recipient of the modified chain, the length of this array must be the same as sizeof(palabra) , that's why char is used as size.

In the loop at the end of my proposal, we created three pointers to origen :

  • palabra : points to the beginning of the original string ( destino ).
  • copia : points to the beginning of the string that will contain the modified copy ( final ).
  • palabra : points to the end of the original string ( origen ).

When the pointer final is equal to the pointer origen != final we know we should stop copying, so the second part of the loop contains the instruction origen since we will copy as long as the final is not origen , otherwise we will advance both the destino and the ++origen, ++destino in the third instruction of the loop: destino .

For each loop turn we will copy EN the address pointed to by origen the original letter of *destino = ... or a letter generated at random (25% of the time); to achieve this we must de-reference the pointer to modify ( *origen ) or read ( %code% ) its content.

You can see an example of the code that I propose [here] .

    
answered by 14.11.2016 в 10:36