0
How do I type any value, whether text or number, and every 3 positions an asterisk be inserted?
Example: type 123456789
, by pressing enter appears: 123*456*789*
0
How do I type any value, whether text or number, and every 3 positions an asterisk be inserted?
Example: type 123456789
, by pressing enter appears: 123*456*789*
2
The logic for what you want to do can be written like this:
Allocate a buffer of 134% of the original size to accommodate the string with asterisks.
Initialize a variable i
zero.
Itere over the original string with the variable j
ranging from zero to strlen(str)
.
Copy a character from the string into j
for the buffer in i
and increment i
:
buffer[i] = str[j];
++i;
If i
is multiple of 4
(i % 4 == 0
) copy an asterisk to the buffer and increment:
buffer[i] = '*';
++i;
Terminate the buffer with a null terminator (buffer[i] = '\0'
) and don’t forget to free up your memory when you’re done using.
0
I think the answer is in your own question:
1 - the person has a place where to enter an entry
2 - when it presisona enter, this value is processed:
2.1 - it picks up everything you typed in;
2.2 - copy 3 characters, insert *, copy the next 3 characters...
3 - at the end take this copy with the * inserted and display to the user.
Browser other questions tagged c
You are not signed in. Login or sign up in order to post.
Hello. What have you tried to do? Telling you how to do it does not seem like solution and the question sounds like homework. Show us what you tried or what you have, but don’t ask us to do the code for you.
– Omni