I wrote a small program as an example to replicate the problem I'm having. The program takes and manipulates two lines in this way:
- It seperates the line by new lines.
- Each line consists of some number of letters followed by a blank (in this case
' '
), followed by a number. - It copies the letters to another string, then adds
'\0'
to it. - It copies the number to another char.
- It prints the string in which are the copied letters and int in which is the converted copied number.
Here is that minimal program:
#include <stdio.h>
#include <string.h>
void read(char *text)
{
// create and initialize the line holder
int k = 0;
char line[10];
line[0] = '\0';
for (int i = 0;text[i] != '\0';i++)
{
if (text[i] == '\n')
{
// k now points to the character right after the last assigned one, so put 0 in that place
line[k] = '\0';
// initialize data objects that will hold text and number
char letters[5];
letters[0] = '\0';
char val;
// step through the line, and stop if you 1. reached a blank or 2. reached the end of a line
int j = 0;
while (line[j] != ' ' && line[j] != '\t' && j <= (strlen(line) - 1))
{
printf("%d <= %ld = %d\n", j, strlen(line) - 1, j <= (strlen(line) - 1));
if (j == (strlen(line) - 1)) // reached the last character before reaching blank
return;
letters[j] = line[j];
j++;
}
letters[j] = '\0'; // where should be blank place 0
if (j + 1 == (strlen(line) - 1)) // if the next character is the last character, meaning that the character before the last one is blank
val = line[j + 1];
else // there is space in that is not one before the last character
return; // this is where read("\n") should stop, but withou entering the while loop!
printf("Word: %s\tVal: %d\n", letters, val - '0');
// empty the line holder
line[0] = '\0';
k = 0;
}
else
{
// place the ith text character into the kth line character and print them
line[k] = text[i];
printf("line[k] = %c\ttext[i] = %c\n", line[k], text[i]);
// increment k for the next turn
k++;
}
}
}
int main()
{
char *text = "ABCD 0\nEFGH 1\nIJKL 2\nMNOP 3\nQRST 4\nUVWX 5\nYZ 5\n";
read(text);
printf("---------------------------------\n");
read("\n");
return 0;
}
There are also the points when this program should terminate without doing its job if it detects an error. Those points are indicated by return
keyword and comments in the read(char *text)
function. There are only two of them, so I'll describe them here as well:
Line 28: Program will stop with scanning on this line if it detected that the current character is the last character. Since the last character should always be preceded with a blank, it means that we reached the end of a line without exiting the while loop (which would have happened if we reached ' '
or '\t'
).
Line 38: If we successfully exited the while loop, the character j
is offset from line
should be a blank. That is because we exited the while loop when we found a blank (that is also because we end the line
with line[j] = '\0'
). That also means that j+1
should be a number, which is the last character in the line. If that is not the case, we reached the blank that is not preceding a number so we exit the function.
So, where's the problem? I passed two strings to
read(char *text)
function, as you can see. read(char *text)
manipulates and prints the first string perfectly. With the second one, which is only "\n"
, the function doesn't work that well. The part that I don't understand is that we enter the while loop, despite the condition j <= strlen(line) - 1)
, which somehow returns 1
when text = "\n"
. You can see that by running the program, it prints that information on line 26.