My code is as shown below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp = fopen("text.txt", "r");
char c;
int d = 0;
char arr[2000];
do {
c = fgetc(fp);
d = d + 1;
if (c == '\n') {
arr[d] = ' ';
} else
arr[d] = c;
} while (c != EOF);
int z = strlen(arr);
arr[0]= '\0';
for (int i = 0;i < z; i++) {
arr[i] = arr[i +1];
}
fclose(fp);
return 0;
}
the code reads a string from a textfile and ands them to an array arr[2000](there are a lot of words in the text file). I wanted to ask if anybody knows a way for the code to read the textfile by each word separated by a space not by character.
So for example if i had an array arr with string : "Jack is a boy"
arr[0] would equal "Jack" not "J"
p.s(The reason for the for loop at the end that removes index 0 is because i keep getting a "(" character at the beginning of the array)
any help would be much appreciated.