#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getstr();
char* addspaces(char* buf);
char* result(char* buf);
int main()
{
char *str=NULL, *res=(char *)malloc(101);
while((str = getstr()) )
{
res = result(str);
printf("Result : %s\n\n", res
);
free(res);
free(str);
}
return 0;
}
char* getstr()
{
char *ptr = (char *)malloc(1);
char buf[101];
int n, len = 0;
*ptr = '\0';
do
{
n = scanf("%100[^\n]", buf);
if(n < 0)
{
free(ptr);
ptr = NULL;
break;
}
if(n == 0)
scanf("%*c");
else
{
len += strlen(buf);
ptr = (char *) realloc(ptr, len + 1);
strcat(ptr, buf);
}
}
while(n > 0);
return ptr;
}
char* addspaces(char* buf)
{
int i, len = 0;
len += strlen(buf);
i = len;
buf = (char *) realloc(buf, len + 3);
buf[i+2] = '\0';
buf[i+1] = ' ';
while(i >= 1)
{
buf[i] = buf[i-1];
i--;
}
buf[0]=' ';
return buf;
free(buf);
}
char* result(char* buf)
{
int i = 0, j = 0, n;
char prev, next, buffer[21], *res;
res = (char *)malloc(101);
*res = NULL;
buf = addspaces(buf);
while(buf[i] != '\0')
{
if ((buf[i] == ' ')||(buf[i] == '\t'))
{
prev = buf[j+1];
next = buf[i-1];
if(prev == next)
{
n = 0;
while(j < i)
{
buffer[n] = buf[j+1];
n++;
j++;
}
buffer[n+1] = '\0';
strncat(res, buffer, n);
}
j = i;
i++;
}
else
i++;
}
return res;
free(res);
}