Hi, i'm trying to do a function to split a char string received by parameter letter-by-letter, store, and return it. I writed this code, its working but i'm using two functions to do that, and i wanna do everything in one function, but using the same code(or almost).
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//this will add a char to string(strcat() only accept same type :( )
void add_char_to_s(char text[], char ch){
size_t sz = strlen(text);
text[sz] = ch;
text[sz + 1] = '\0';
}
//now lets split received word by spaces and return it
static char *xplode_string(char word[])
{
static char t[sizeof(*word)] = "";
for (int i=0; i < strlen(word); i++)
{
add_char_to_s(t, word[i]);
strcat(t, " ");
}
return t;
}
int main() {
//this will print P a o l a!
printf("%s", xplode_string("Paola!"));
return 0;
}