0

I'm parsing a text file:

Hello, this is a text file.

and creating by turning the file into a char[]. Now I want to take the array, iterate through it, and create an array of arrays that splits the file into words:

 string[0] = Hello
 string[1] = this
 string[2] = is

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TextReader.h"
#include <ctype.h>

void printWord(char *string) {
int i;
for (i = 0; i < strlen(string); i ++)
    printf("%c", string[i]);
printf("\n");
}

void getWord(char *string) {
char sentences[5][4];
int i;
int letter_counter = 0;
int word_counter = 0;

    for (i = 0; i < strlen(string); i ++) {
            // Checks if the character is a letter
    if (isalpha(string[i])) {
        sentences[word_counter][letter_counter] = string[i];
        letter_counter++;
    } else {
        sentences[word_counter][letter_counter + 1] = '\0';
        word_counter++;
        letter_counter = 0;
    }
}

// This is the code to see what it returns:
i = 0;
for (i; i < 5; i ++) {
    int a = 0;
    for (a; a < 4; a++) {
        printf("%c", sentences[i][a]);
    }
    printf("\n");
}
}

int main() {
    // This just returns the character array. No errors or problems here.
char *string = readFile("test.txt");

getWord(string);

return 0;
}

This is what it returns:

Hell
o
this
is
a) w

I suspect this has something to do with pointers and stuff. I come from a strong Java background so I'm still getting used to C.

2

2 Answers 2

3

With sentences[5][4] you're limiting the number of sentences to 5 and the length of each word to 4. You'll need to make it bigger in order to process more and longer words. Try sentences[10][10]. You're also not checking if your input words aren't longer than what sentences can handle. With bigger inputs this can lead to heap-overflows & acces violations, remember that C does not check your pointers for you!

Of course, if you're going to use this method for bigger files with bigger words you'll need to make it bigger or allocate it dymanically.

0
0

sample that do not use strtok:

void getWord(char *string){
    char buff[32];
    int letter_counter = 0;
    int word_counter = 0;
    int i=0;
    char ch;

    while(!isalpha(string[i]))++i;//skip
    while(ch=string[i]){
        if(isalpha(ch)){
            buff[letter_counter++] = ch;
            ++i;
        } else {
            buff[letter_counter] = '\0';
            printf("string[%d] = %s\n", word_counter++, buff);//copy to dynamic allocate array
            letter_counter = 0;
            while(string[++i] && !isalpha(string[i]));//skip
        }
    }
}

use strtok version:

void getWord(const char *string){
    char buff[1024];//Unnecessary if possible change
    char *p;
    int word_counter = 0;

    strcpy(buff, string);
    for(p=buff;NULL!=(p=strtok(p, " ,."));p=NULL){//delimiter != (not isaplha(ch))
        printf("string[%d] = %s\n", word_counter++, p);//copy to dynamic allocate array
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.