알고리즘 9 - Remove String Spaces

jabae·2021년 10월 5일
0

알고리즘

목록 보기
12/97

Q.

Simple, remove the spaces from the string, then return the resultant string.

For C, you must return a new dynamically allocated string.

A)

#include <stdlib.h>

char *no_space(const char *str_in) {
  int len = 0;
  while (str_in[len] != '\0')
    len++;
  char *result = malloc(sizeof(char) * len + 1);
  int i = 0;
  int j = 0;
  while (str_in[i] != '\0')
  {
    if (str_in[i] == ' ')
      i++;
    else
     result[j++] = str_in[i++];
  }
  while (j <= len)
    result[j++] = '\0';
  return (result);
}
profile
it's me!:)

0개의 댓글