You extracted the first word in this statement
const char *token = strtok_s(testString, ", ", &context);
but you did not print it. You are starting to print words in the while loop after the preceding call of strtok_s within the loop.
You need two pointers that will point to adjacent extracted strings and using these pointers you can compare first letters of the strings.
Here is a demonstrative program (for simplicity I am using strtok instead of strtok_s)
#include <stdio.h>
#include <string.h>
int main(void)
{
char testString[] = "In the end, we will remember not the words "
"of our enemies, but the silence of our friends";
char *first_word = NULL;
char *second_word = NULL;
const char *delim = ", ";
if ( ( first_word = strtok( testString, delim ) ) != NULL )
{
while ( ( second_word = strtok( NULL, delim ) ) != NULL &&
*first_word != *second_word )
{
first_word = second_word;
}
}
if ( second_word != NULL )
{
printf( "%s <-> %s\n", first_word, second_word );
}
return 0;
}
The program output is
we <-> will
If you want to output all such pairs of words then the program can look the following way
#include <stdio.h>
#include <string.h>
int main(void)
{
char testString[] = "In the end, we will remember not the words "
"of our enemies, but the silence of our friends";
char *first_word = NULL;
char *second_word = NULL;
const char *delim = ", ";
if ( ( first_word = strtok( testString, delim ) ) != NULL )
{
while ( ( second_word = strtok( NULL, delim ) ) != NULL )
{
if ( *first_word == *second_word )
{
printf( "%s <-> %s\n", first_word, second_word );
}
first_word = second_word;
}
}
return 0;
}
The program output is
we <-> will
of <-> our
of <-> our
However instead of using strtok
or strtok_s
it is much better to use an approach based on the functions strspn
and strcspn
. In this case you can process constant strings.