I'm looking for a way to iterate over each word within a string gathered from the getchar()
function in C. I can't use pointers. Each word in the sentence will have something done to it but should not be affected by the other words and I need a way to access the letters, such as even if I'm at the third word, buffer[k]
if k = 0
would give the first letter of the third word.
void read_line (char buffer[])
{
char character;
int i = 0;
for (i = 0; i < 32; ++i)
{
character = getchar ();
buffer[i] = character;
}
}
Normally you would use strchr to find a space in the string, but since you can't use pointers:
void read_line (char buffer[])
{
char character;
int i = 0;
for (i = 0; i < 32; ++i)
{
character = getchar ();
if (character == '\n')
break;
buffer[i] = character;
}
i = 0;
while (buffer[i] != '\n')
{
for (; buffer[i] != ' '; ++i)
// each iteration of this loop will be on the same word
++i;
}
}