Code:
// Structure declaration and type definition
struct wordsRec {
int type;
// 0 represents non-number, 1 represents numbers.
char value[4];
};
typedef struct wordsRec Words;
int findWords(char *string, Words *outputWords)
{
int i;
int numberOfWords = 0;
int currentChar = 0;
int length;
int extraSpaceCheck = 1; // Catches multiple blank spaces
printf("strlen(string): %d\n", strlen(string));
length = strlen(string);
// Sorts the string into seperate 'words'
for (i = 0; i < length; i++) {
if (string[i] != ' ') {
extraSpaceCheck = 0;
if (currentChar == 4) {
fprintf(stderr, "Exceeded word limit. Program terminating.\n");
exit(1);
}
outputWords[numberOfWords].value[currentChar++] = string[i];
if (i == length - 1) outputWords[numberOfWords].value[currentChar] = '\0';
}
else if (extraSpaceCheck == 0) {
outputWords[numberOfWords].value[currentChar] = '\0';
currentChar = 0;
numberOfWords++;
extraSpaceCheck = 1;
}
}
numberOfWords++;
// Determines if the 'words' are integers or characters
for (i = 0; i < numberOfWords; i++) {
outputWords[i].type = 1;
length = strlen(outputWords[i].value);
printf("length: %d\n", length);
for (currentChar = 0; currentChar < length; currentChar++) {
if (!(outputWords[i].value[currentChar] == '-' && currentChar == 0)
&& !(outputWords[i].value[currentChar] >= 48 && outputWords[i].value[currentChar] <= 57)) {
outputWords[i].type = 0;
break;
}
}
// printf("str: %s, %d | %s, %d\n", outputWords[i].value, strlen(outputWords[i].value), outputWords[i - 1].value, strlen(outputWords[i - 1].value));
}
// Fixes the 'suddenly changing strlen' problem; artificial solution
for (i = 0; i < numberOfWords; i++) if (strlen(outputWords[i].value) > 4) outputWords[i].value[4] = '\0';
// Returns the number of words
return numberOfWords;
}
The section of the function findWords() where I determine if a word is a number or not, increments the string length of the 'word'. This only happens if the word is 4 character in length (which is the defined max chars in a word), and the word following it is a number. I've looked through my code alot of times and I can't seem to figure out why this is happening.
Any help would be appreciated, and if my code is unclear, I'd be more than happy to elaborate
Thanks in advance.