#include <iostream>
using namespace std;
// get string length
int StrLen(char a[]);
// copy array b to array a
void StrCopy(char a[], char b[]);
// add array b to the end of array a
void StrCat(char a[], char b[]);
// compare array b to array a for a match
int StrComp(char a[], char b[]);
// converts string to uppercase
void GoUpper(char a[]);
// converts strig to lowercase
void GoLower(char a[]);
// string a is greater than string b
bool StrGreat(char a[], char b[]);
// string a is less than string b
bool StrLess(char a[], char b[]);
// string a is equal to string b
bool StrEqual(char a[], char b[]);
// find a single character in a string
int Find(char myString[], char findThis, int pos);
// find any character in the character set within the string
int FindAny(char myString[], char findThese[], int pos);
// find any character in the string that's not in the character set
int FindNotAny(char myString[], char findThese[], int pos);
// creates a new string from an exisiting string
void SubStr(char myString[], char subString[], int start, int length);
// requires lower level function: FindAny (gets the beginning index of the substring)
// requires lower level function: FindNotAny (gets the ending index of the substring)
// subtracting the value of FindNotAny from FindAny returns the length of the substring
// requires lower level function: SubStr (passing the length (FindNotAny - FindAny = length) will produce the desired substring)
// Zorg returns an integer which is the current index of the string
int Zorg(char myString[], char charSet[], char subString[], int pos);
// requires lower level function: Find (looks at the first character to determine it's type)
// token types are a: letters, n: numbers, s: special, w: whitespace
// requires lower level function: Zorg (creates tokens(substrings) and updates index
bool GetToken(char Block[], char Token[], char &tokenType, int &pos);
// token blocks
void GetTokenizedBlock(char words[][50], char block[], char token[], char &tokenType, int &pos);
int main() {
char block[1000] = "running in the woods.";
char token[1000];
char words[25][50];
char tokenType;
int pos = 0;
cout << block << endl;
GetTokenizedBlock(words, block, token, tokenType, pos);
cout << words[0] << endl;
cout << words[1] << endl;
cout << words[2] << endl;
cout << words[3] << endl;
return 0;
}
void GetTokenizedBlock(char words[][50], char block[], char token[], char &tokenType, int &pos) {
// return all tokens
while (GetToken(block, token, tokenType, pos)) {
if (tokenType != 'w') {
for(int a = 0; a < pos; a++) {
StrCopy(words[a], token);
}
}
}
}
// function gettoken
bool GetToken(char Block[], char Token[], char &tokenType, int &pos) {
// token type details
char alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char numeric[] = "1234567890";
char special[] = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
char whitespace[] = "\t\n ";
// stop once the
if (pos == -1) {
return false;
}
// determines the token type
// searches token type string for a match(greater than -1)
if (Find(alpha, Block[pos], 0) != -1) {
// alpha type
tokenType = 'a';
// updated position
pos = Zorg(Block, alpha, Token, pos);
} else if (Find(numeric, Block[pos], 0) != -1) {
// number type
tokenType = 'n';
// updated position
pos = Zorg(Block, numeric, Token, pos);
} else if (Find(special, Block[pos], 0) != -1) {
// special type
tokenType = 's';
// updated position
pos = Zorg(Block, special, Token, pos);
} else if (Find(whitespace, Block[pos], 0) != -1) {
// white space type
tokenType = 'w';
// updated position
pos = Zorg(Block, whitespace, Token, pos);
} else {
return false;
}
return true;
}
// function zorg
int Zorg(char myString[], char charSet[], char subString[], int pos) {
// start of token
int start = FindAny(myString, charSet, pos);
// end of token
int end = FindNotAny(myString, charSet, start);
// update index for next token
int length = end - start;
// makes a token(substring) from the string
SubStr(myString, subString, start, length);
// the position in the string after the token is found
return end;
}
// substring function (takes a portion of a string and creates a new string)
void SubStr(char myString[], char subString[], int start, int length) {
// variable declaration
int a = 0, // starting position for subString
b = start; // starting position for myString
// while starting position is less than length
while (a <= (length - 1) && b <= StrLen(myString)) {
// each position b on myString is set to positions a on subString
// as long as it is between start and length
subString[a] = myString[b];
// increment positions for myString and subString
a++;
b++;
}
// set null to subsring
subString[a] = '\0';
}
// function string length
int StrLen(char a[]) {
// variable declaration
int i = 0; // index starts at 0
// while array a is now null (for each element in the array)
while (a[i] != '\0') {
// increment the index
i++;
}
// returns the last index of the array
return i;
}
// function copy
void StrCopy(char a[], char b[]) {
// variable declaration
int i = 0; // index starts at 0
// while array b is not null (for each element in array b)
while (b[i] != '\0') {
// add array b value to array a
a[i] = b[i];
// increment index
i++;
}
// set array to null
a[i] = '\0';
}
// function concatenate
void StrCat(char a[], char b[]) {
// variable declaration
int i = StrLen(a), // length of array a
u = 0; // index for array b
// while array b is not null (for each element in array b)
while (b[u] != '\0') {
// add array b value to array a
a[i] = b[u++];
// increment index
i++;
}
// set array a to null
a[i] = '\0';
}
// function compare
int StrComp(char a[], char b[]) {
// variable declaration
int i = 0;
// a is equal to b and bot are
while (a[i] == b[i] && a[i] != '\0' && b[i] != '\0') {
i++;
}
// a is greater than b
if (a[i] > b[i]) {
return 1;
}
// a is less than b
if (a[i] < b[i]) {
return -1;
}
return 0;
}
// convert to uppercase
void GoUpper(char a[]) {
// variable declaration
int i = 0;
while (a[i] != '\0') {
// if characters are between a and z
if (a[i] >= 'a' && a[i] <= 'z') {
// subtract ' ' converts to upper
a[i] -= ' ';
// increment
i++;
}
}
}
// convert to lowercase
void GoLower(char a[]) {
// variable declaration
int i = 0;
while (a[i] != '\0') {
// if characters are between A and Z
if (a[i] >= 'A' && a[i] <= 'Z') {
// adding ' ' converts to lower
a[i] += ' ';
// increment
i++;
}
}
}
// greater than compare
bool StrGreat(char a[], char b[]) {
if (StrLen(a) > StrLen(b)) {
return true;
} else {
return false;
}
}
// less than compare
bool StrLess(char a[], char b[]) {
if (StrLen(a) < StrLen(b)) {
return true;
} else {
return false;
}
}
// equal compare
bool StrEqual(char a[], char b[]) {
if (StrLen(a) == StrLen(b)) {
return true;
} else {
return false;
}
}
// find a key
int Find(char myString[], char findThis, int pos) {
// variable declaration
int i = pos; // starting position
// loop string
while (myString[i] != '\0') {
// if match
if (myString[i] == findThis) {
return i;
}
// increment
i++;
}
return -1;
}
// find any key
int FindAny(char myString[], char findThese[], int pos) {
// variable declaration
int a = pos; // starting position
// loop string
while (myString[a] != '\0') {
// loop keys
for (int b = 0; b < StrLen(findThese); b++) {
if (myString[a] == findThese[b]) {
return a;
}
}
// increment myString position
a++;
}
// return -1 if nothing is found
return -1;
}
// find not any function (look for anything as long as it's not part of findThese)
int FindNotAny(char myString[], char findThese[], int pos) {
// variable declaration
int a = pos; // starting position for myString
bool keeper; // keep track if anything is found
// loop myString
while (myString[a] != '\0') { // while myString is not null
//
keeper = false;
// loop findThese and compare to myString for a match and return true
for (int b = 0; b < StrLen(findThese); b++) {
if (myString[a] == findThese[b]) {
keeper = true;
}
}
//
if (keeper != true) {
return a;
}
// increment myString position
a++;
}
// return -1 if nothing is found
return -1;
}