here is the code:
#define _GNU_SOURCE
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
string alphabet = "abdcefghijklmnopqrstuvwxyz";
string text = "world";
string ciphertext = "";
for(int i = 0; i < strlen(text); i++)
{
ciphertext = strstr(alphabet, &text[i]);
printf("%s \n", ciphertext);
}
It outputs the following result:
(null)
(null)
(null)
(null)
dcefghijklmnopqrstuvwxyz
So it looks like strstr() works only for the last character, in this case "d". Why it does not work for prior characters? strcasestr() has the same behavior
Because you want to find character in the string, not string in the string, you need to use strchr
function:
for(size_t i = 0; text[i]; i++)
{
ciphertext = strchr(alphabet, text[i]);
printf("%s \n", ciphertext);
}
size_t
)strlen
on every iteration. It is enough to check if you did not reach the null terminating character.