How to insert an asterisk every 3 characters?

Asked

Viewed 171 times

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*

  • 5

    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.

2 answers

2

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

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

  2. Initialize a variable i zero.

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

    1. Copy a character from the string into j for the buffer in i and increment i:

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

      buffer[i] = '*';
      ++i;
      
  4. 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

You are not signed in. Login or sign up in order to post.