0

Please help me with some advice on completing this problem. I can print out the first letter of someone's first name, but I do not know how to go about printing out the first letter in someone's middle name or last name. Any help is greatly appreciated. My problem is getting to print out the first letter in the middle name because I have trouble getting through the first spaces and alphabet characters before the middle name. Again, any help is greatly appreciated.

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
string s = get_string();

if (s != NULL)
{
    for (int i = 0, n = strlen(s); i < n; i++)
    {
         if (s[i] != ' ' && isalpha(s[i]))
        {
            printf("%c", toupper(s[i]));
            break;
        }
    }

    for (int j = 0, n = strlen(s); j < n; j++)
    {
        if (s[j] == ' ' && isalpha(s[j]+1))
        {
            printf("%c", s[j]++);
        }
    }



    printf("\n");
}
}
0

You have mistakes in incrementing indexes of an array's elements.
Writing

s[j]+1

or

s[j]++

means increment the value that exist in the j-th element. If you need to do operation on index itself, you may do this in the square brackets, so try to change above to the

s[j+1]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .