#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int SplitMsgReverse(const char* str_src)
{
char* str_tok = NULL;
int space = 0;
int len_str = 0;
int len_tok = 0;
int len_mem = 0;
char* it = (char*)str_src;
char* bgn = it;
char* end = it;
for(; '\0' != *it; ++it)
{
if(' ' == *it)
{
// find max length token
end = it;
len_tok = end - bgn;
bgn = it;
if(len_mem <len_tok)
len_mem = len_tok;
// increase space count
++space;
}
}
// length test
len_str = it - str_src;
if(0 == len_str)
return 0;
// no space
if(0 == space)
{
printf("%s\n", str_src);
return 0;
}
if('\0' == *it)
--it;
if('\n' == *it)
--it;
// search end to begin
end = it+1;
bgn = NULL;
// makeup the 4-multiple memsize
str_tok = (char*)malloc( int( (len_mem+11)/4 ) * 4);
for(; it >=(char*)str_src; --it)
{
if(' ' == *it)
{
bgn = it+1;
len_tok = end-bgn;
if(1 < len_tok)
{
memset(str_tok, 0, len_tok+2);
strncpy(str_tok, bgn, len_tok);
printf("%s\n", str_tok);
}
end = it;
}
}
++it; // over bgin..
if(' ' != *it)
{
bgn = it;
len_tok = end-bgn;
if(1 < len_tok)
{
memset(str_tok, 0, len_tok+2);
strncpy(str_tok, bgn, len_tok);
printf("%s\n", str_tok);
}
end = it;
}
free(str_tok);
return 0;
}
int main()
{
while(1)
{
char str_src[4096+4]={0};
//char str_src[4096+4]=" 안녕 하세요 123456 7890ABCDEF hell lo wel come !!! to day hu mor \n";
printf("1000자 미만의 문장을 입력하세요 : ");
fgets(str_src, 4096, stdin);
if(feof(stdin)) // for ctrl-z
break;
SplitMsgReverse(str_src);
}
return 0;
}