Rad Rad - 1 year ago 93
C Question

Best way to read a file into buffer if you need to check middle symbol of word

Michi has answered my question in the comments below.

I want to ask what's the best way of reading a file (pref. using buffer) if I will need to throw out words which middle symbol is a number. (There can be more than one space between words). The text file could look like this "asd4ggt gklk6k k77k 345k ll4l 7" so I need to throw out "asd4ggt" and "7" (I don't need to throw out "k77k" because it's even number of symbols so there isn't middle symbol). In words symbols can be from 0 to 9, A to Z, a to z (only simple English alphabet)

I think of reading a text file word by word: read one word into buffer if it has even number of symbols then write it to file but if it has odd number of symbols then I have to check if its' middle symbol is a number and if it is I skip this word and go to the next word.
Is this a right way of thinking how to complete this task?

Answer Source

Based of your comment we came to this:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int evenCheck(const char *ptr);
size_t middleCheck(const char *ptr);

int main(void){
    const char *ptr = "t4k4k";
    size_t middle = middleCheck(ptr);

    if( evenCheck(ptr) == 0){
        printf("Output to file the word %s\n",ptr);
        if ( isdigit(ptr[middle]) ){
            printf("Ignoring the word %s, because has the number %c in the middle\n",ptr, ptr[middle]);
            printf("Output to file the word %s, because the middle is %c which is a Letter\n",ptr, ptr[middle]);

int evenCheck(const char *ptr){
    size_t len = strlen(ptr);

    if ( (len % 2) ){
       return 1;

    return 0;

size_t middleCheck(const char *ptr){
    size_t middle = strlen(ptr) / 2;

    return middle;


Output to file the word t4k4k, because the middle is k which is a Letter

Now you were asking about how to do this if the file has more than one word.

Well one option will be to save the file in a Multi-Dimensional array or read the whole file.

I'm sure you can do it, if not come back with another Question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download