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*
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*
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.
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.