I am learning C but I am still a noob. I am writing a program as an exercise on dynamic memory allocation that takes text from the user with unknown length and gives back this text with no spaces, tabs, special characters or numbers. The program seems to work fine except that some of the text seems to be changed to unknown characters for unknown reasons. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *pstring;
pstring = (char *)malloc( sizeof(char));
char c = '$';
int i = 0;
while(c != '\n')
{
c = getchar();
if(isalpha(c))
{
pstring[i] = c;
realloc(pstring, (i+2)*sizeof(char));
i++;
}
}
int j;
for(j = 0; j < i; j++)
{
printf("%c", pstring[j]);
}
return 0;
}