0

So here is my current approach. I'm struggling with printing only the first character and the first character after a space in the problem. I feel like I'm pretty close but can't seem to figure out this last part.

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

int main (void)
{           //Prompt user for string, define variables
        int i;
        string name = get_string();
        //Iterate through each chacter of the string
        for (i = 0; i < strlen(name); i++){
            if (name[0]){

       printf("%c", toupper(name[i]));
 }
 }
 }

Thank you all for your help!

1

You should print uppercased name[0] if it's not a space and is either the first character or following a space.

If it's the first character, there's no previous element, but conveniently, the || operator uses short-circuit evaluation. If left side is true, it won't bother evaluating its right side. Same for && if left side is false.

Note that && operator has higher precedence than ||, you might need parentheses.

You must log in to answer this question.

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