Algorithm 22 - Function 1 - hello world

Beast from the east·2021년 10월 6일
0

Algorithm

목록 보기
22/27

Q.

Description:
Description:
Make a simple function called greet that returns the most-famous "hello world!".

Style Points
Sure, this is about as easy as it gets. But how clever can you be to create the most creative hello world you can think of? What is a "hello world" solution you would want to show your friends?

A)

//Write a function `greet` that returns "hello world!"
char *greet(void)
{
  char *ptr = "hello world!";
  return ptr;
}

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

char *greet()
{
  char *code = "++++++++++[>++++++++++>+++++++++++>++++++++++++>+++<<<<-]>++++.---.>--..+++.>>++.<-.<.>-----.<---.<-.>>>+.";

  char byteArr[] = {0x00, 0x00, 0x00, 0x00, 0x00};
  int pointer = 0;
  int outPointer = 0;
  char out[14] = "";
    int length = (int)strlen(code);
  for (int i = 0; i < length; i++)
  {
    char chr = code[i];
    switch (chr)
    {
    case '>':
      pointer++;
      break;

    case '<':
      pointer--;
      break;

    case '+':
      byteArr[pointer]++;
      break;

    case '-':
      byteArr[pointer]--;
      break;

    case '.':
      out[outPointer] = byteArr[pointer];
      outPointer++;
      break;

    case '[':
      if (byteArr[pointer] == 0x00)
        for (int j = 0; j < length; j++)
          if (code[j] == ']')
            i = j;
      break;

    case ']':
      if (byteArr[pointer] != 0x00)
        for (int j = 0; j < length; j++)
          if (code[j] == '[')
            i = j;
      break;
    }
  }

  char* result = malloc(strlen(out) + 1);

    strcpy(result, out);

    return result;
} -> ??
profile
Hello, Rabbit

0개의 댓글