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 == ';'));
}