Hello everyone.
I'm trying to write a method that will read text from a file, change any punctuation or numbers to spaces and also preserve the existing spaces between words. I've got this far:
Code:
char * getText()
{
FILE * src = fopen("concl.txt","r");
char * str = malloc(5001);
char next,last=(char)"";
int check,i=0;
while((next=fgetc(src)) != EOF)
{
check = next;
if(isalpha(check))
str[i] = next;
else {
if(!isspace(last))
str[i] = ' ';
}
i++;
last = next;
}
return str;
}
I'm also checking that if the previous character was a space it's not going to add extra space if, for example, it finds " !". However, as soon as it encounters a double space it stops adding anything to the array and, obviously, gives an incomplete output. Can someone set me on the right track?