How to insert an asterisk every 3 characters?

0

How do I enter any value, either text or number, and an asterisk is inserted every 3 positions?

Example: I type 123456789 , pressing enter will show: 123*456*789*

    
asked by anonymous 18.06.2014 / 20:46

2 answers

2

The logic for what you want to do can be written like this:

  • Allocate a buffer with 134% of the original size to accommodate the string with asterisks.

  • Initialize a variable i with value zero.

  • Iterate over the original string with the variable j ranging from zero to strlen(str) .

  • Copy a string character in j to the buffer in i and increase i :

    buffer[i] = str[j];
    ++i;
    
  • If i is a multiple of 4 ( i % 4 == 0 ) copy an asterisk to the buffer and increment:

    buffer[i] = '*';
    ++i;
    
  • Finish the buffer with a null terminator ( buffer[i] = '%code%' ) and do not forget to free up the memory when you finish using it.

  • 18.06.2014 / 22:44
    0

    I think the answer lies in your own question:

    1 - the person has a place to enter an entry

    2 - When it presses enter, this value is processed:

    2.1 - it takes everything you typed;

    2.2 - copy 3 characters, insert a *, copy the next 3 characters ...

    3 - at the end you get this copy with the * inserted and displayed for the user.

        
    18.06.2014 / 21:18