to day hu mor 이런 문장이 있으면 mor hu day to 이렇게 출력하는 프로그램입니다.
출력이 되긴 하는데, 자꾸 문장 제일 앞에 쓰레기값이 붙어요. t뛟mor hu day to 이런 식으로요
널값을 안 넣어준 것도 아니고.. 왜 그런지조차 모르겠습니다. 도와주세요..
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int space=0, i, a=0, b=0;
char boy[1000], *length, **cut;
printf("1000자 미만의 문장을 입력하세요 : ");
gets(boy);
length =(char*)malloc(strlen(boy)+1);
length = boy;
for(i=0; i<strlen(boy)+1; i++)
{
if (boy[i] == ' ')
{
space++;
}
}
if (space == 0) //공백이 없을 때
{
printf("%s", boy);
return 0;
}
cut = (char**)malloc(space+1);
for(i=0; i<space+2; i++)
{
cut[i]=(char*)malloc(strlen(boy)+1);
} //메모리 할당
for(i=0; i<strlen(boy)+2; i++)
{
cut[space+1-a][b] = boy[i];
if (boy[i]==NULL)
{
cut[space+1-a][b] = ' ';
cut[space+1-a][b+1]=NULL;
break;
}
if (boy[i]==' ')
{
cut[space+1-a][b+1]=NULL;
a++;
b=-1;
}
b++;
}
for(int x=0; x<space+2; x++)
{
printf("%s", cut[x]);
}
return 0;
}