I've managed to whip up a solution in Python 3 that seems to work.
name = input("")
initials = name[0]
for i in range(0, len(name)):
if (name[i].isspace()):
initials += name[i + 1]
print(initials.upper())
However, I'm having trouble translating the syntax from Python 3 to C.
#include <cs50.h> #include <stdio.h> #include <string.h>
int main(void) { string name = get_string(); char initials = name[0]; // What data type should I use for this? for (int i = 0, len = strlen(name); i < len; i++) { if (name[i] == '\0') { initials = initials + name[i + 1]; // Python 3 by default // takes a string to be a // char array that can be appended } }
printf("%c\n", initials); }
Python 3 takes string as a character array that can be easily appended to. How can I go about doing the same with C?
if (name[i]…
is the only place you do any work, but you're only entering that section ifname[i] == '\0'
which means you reached a null character, the end of string