Henrick
Henrick

Reputation: 81

How can I duplicate a letter in a string?

I want to write a program where it duplicates every letter in a given string.

For example, if the input is abc then the output would be aabbcc.

How can I do this?

Here is my code so far. It only copies the string:

#include <stdio.h>

int main () {
    char str_in[100];
    char str_out[200] = "";
    int i;

    printf("Enter a word: ");
    scanf("%s", str_in);

    for (i = 0; i < sizeof(str_in); i++) {
        str_out[i] += str_in[i];
    }

    printf("Duplicated word: %s", str_out);

    return 0;
}

Upvotes: 1

Views: 409

Answers (4)

Vineet1982
Vineet1982

Reputation: 7918

#include <stdio.h>

int main () {
    char str_in[100];
    char str_out[200] = "";
    int i, j;

    printf("Enter a word: ");
    scanf("%s", str_in);

   j=0;
   for (i = 0; i < sizeof(str_in); i++) {
       str_out[i] = str_in[j];
       str_out[i+1] = str_in[j];
       i++;
       j++;
   }

   printf("Duplicated word: %s", str_out);

   return 0;
}

Upvotes: 0

S.S. Anne
S.S. Anne

Reputation: 15586

I would suggest only allocating the amount of memory you need:

char *str_out = malloc(strlen(str_in) + 1);

and then using another loop variable that counts by 2 each time so you have 2 times as much space for every character in your loop.

If you do that, you should also put + 1 next to the new loop variable to copy it to the current and next elements of the string, thereby duplicating the characters.

I would also advise you to use size_t for your loop variables to match the return of str_len.

Here I have implemented my suggestions:

size_t str_ctr;
size_t str_ctrx2;
size_t in_str_len = strlen(in_str)
/* ... */
for( str_ctrx2 = str_ctr = 0;
     str_ctr < in_str_len;
     str_ctr++, str_ctrx2 += 2 )
{
    out_str[str_ctrx2] = in_str[str_ctr];
    out_str[str_ctrx2 + 1] = in_str[str_ctr];
}
/* null-terminate the string */
out_str[str_ctrx2] = '\0';

I would also like to mention that you forget to null-terminate your string in your program. I have made a comment about that and have shown you how to do it above.

Upvotes: 0

Saucy Goat
Saucy Goat

Reputation: 1675

This is one way to do what you want - since you know that the two indexes in str_out that you want to access correspond to the ith and i+1th positions of str_in:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define SIZE    100


int main(void)
{
    char str_in[SIZE];
    char str_out[SIZE*2];
    int i, len;

    printf("Enter a word: ");
    scanf("%s", str_in);
    len = strlen(str_in);

    for (i = 0; i < len; i++) {
        str_out[i*2] = str_in[i];
        str_out[i*2+1] = str_in[i];
    }
    str_out[i*2] = '\0';

    printf("Duplicated word: %s\n", str_out);

    return EXIT_SUCCESS;
}

You could also use another variable and update inside the loop as such:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define SIZE    100


int main(void)
{
    char str_in[SIZE];
    char str_out[SIZE*2];
    int i, j, len;

    printf("Enter a word: ");
    scanf("%s", str_in);
    len = strlen(str_in);

    for (i = 0, j = 0; i < len; i++, j += 2) {
        str_out[j] = str_in[i];
        str_out[j+1] = str_in[i];
    }
    str_out[j] = '\0';

    printf("Duplicated word: %s\n", str_out);

    return EXIT_SUCCESS;
}

As a side note, it's best to #define your constants instead of having them in your code.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

For starters the destination character array should be at least two times larger than the source array.

The loop that performs the copying can look the following way

size_t j = 0;
for ( size_t i = 0; str_in[i] != '\0'; i++ ) 
{
    str_out[j++] = str_in[i];
    str_out[j++] = str_in[i];
}

str_out[j] = '\0';

Upvotes: 1

Related Questions