CodeWars Logo

CodeWars C Kata - Double Char

By Lochard | As a newbie programmer | 12 Jul 2023


DESCRIPTION:

Given a string, you have to return a string in which each character (case-sensitive) is repeated once.

 

Examples (Input -> Output):

* "String" -> "SSttrriinngg"

* "Hello World" -> "HHeelllloo WWoorrlldd"

* "1234!_ " -> "11223344!!__ "

char *double_char (const char *string, char *doubled)
{
  char *writing_head = doubled;
  char *reading_head = string - 1;
  while (*++reading_head) {
    *writing_head++ = *reading_head;
    *writing_head++ = *reading_head;
  }
  *writing_head = '\0';

  return doubled; // return it
}

How do you rate this article?

3


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.