I need to read a file and then print the first letter of each word in lowercase and their position in the file. I made this code but it's not working and i don't know why.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int isLetterOrNumber(int c)
{
if (((c >= 48) && (c <= 57)) || ((c >= 97) && (c <= 122)))
return 1;
else
return 0;
}
int main()
{
int position, c, pc;
position = 0;
FILE *file = fopen("text.txt", "r");
if (! file){
printf("Failed to open text.txt.\n");
return 1;
}
while ((c = fgetc(file)) != EOF){
if (position == 0){
if (isLetterOrNumber(c)){
printf("%c %d", c, position);
position++;
}
break;
}
pc = fseek(file, -1, SEEK_CUR);
c = tolower(c);
pc = tolower(pc);
if (isLetterOrNumber(c)){
if (isLetterOrNumber(pc)){
printf("%c %d", c, position);
position++;
}
}
}
}
Im trying to verify if the current character and the previous character is a letter or number and then I print it alongside its position in the text.
The file that i need to read is a really big book, it has a lot of characters that are not numbers or letters and i think that im missing something that has to do with that... i'm not sure