How to append a char into array and tokenize it C

Viewed 193

In my main function I am using letter(ch). I am trying to get some letter characters to become a string or token. Before using the append function it would just print all the characters out.

//output  
000: u  
001: s  
002: e  
003: .  
004: s  
005: y  
006: s  
007: t  
008: e  
009: m  
010: .  
011: i  
012: o  
013: ;  

I found an appending function that gets the letter characters into the array, but it prints out like a pyramid. How do I get the last result when it's done appending, and use the '\0' to split it? Shouldn't it have already split it by the initial '\0' in the append function? Am I approaching this in the right direction? Is there something I'm missing in order for the split after the null?

//output   
000: u  
001: us  
002: use  
003: .  
004: uses  
005: usesy  
006: usesys  
007: usesyst  
008: usesyste  
009: usesystem  
010: .  
011: usesystemi  
012: usesystemio  
013: ;  

//main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include "ch_type.h"


FILE *file;
char line[400];
int line_num;
char  ch;

void append(char* line, char ch)
{
    int length = strlen(line);
    line[length] = ch;
    line[length + 1] = '\0';
}

int main()
{
    file = fopen("source.txt", "r");

    while((ch = fgetc(file)) != EOF)
    {
        if(space(ch))
        {
            printf(" %03d: %c\n", line_num++, ch);
        }
        if(letter(ch))
        {
            append(line, ch);
            printf(" %03d: %s\n", line_num++, line);
        }
        if(separator(ch))
        {
            printf(" %03d: %c\n", line_num++, ch);
        }
    }
    return 0;
}

//ch_type.h

int space(int ch)
{
    return ((ch == ' '));
}

int letter(int ch)
{
    return (((ch >= 'a') && (ch <= 'z')) || (ch >= 'A') && (ch <= 'Z'));
}


int separator(int ch)
{
    return ((ch == '#') || (ch == '(') || (ch == ')') || (ch == '{') || (ch == '}') || (ch == '[') || (ch == ']')
        || (ch == '<') || (ch == '>') || (ch == '.') || (ch == ',') || (ch == ':') || (ch == ';'));
}
1 Answers

Print line when you enter if(space(ch)) or if(separator(ch)) instead of after finding each character of a line. After that, set the first byte to 0 so the string is "empty" again:

        if(space(ch))
        {
            printf(" %03d: %s\n", line_num++, line);
            line[0] = '\0';
            printf(" %03d: %c\n", line_num++, ch);
        }
        if(letter(ch))
        {
            append(line, ch);
        }
        if(separator(ch))
        {
            printf(" %03d: %s\n", line_num++, line);
            line[0] = '\0';
            printf(" %03d: %c\n", line_num++, ch);
        }

You should probably also prepend else to both if(letter(ch)) and if(separator(ch)). This might improve performance a little since none of these conditions overlap.

Note that this is assuming that separators and spaces don't go together, but that shouldn't necessarily matter.

Here is a simplified version of this that doesn't require duplicated code:

        if(letter(ch))
        {
            append(line, ch);
        }
        else
        {
            printf(" %03d: %s\n", line_num++, line);
            line[0] = '\0';
        }
        if(space(ch) || separator(ch))
        {
            printf(" %03d: %c\n", line_num++, ch);
        }
Related