0

Ok so I am stuck on pset2 initials. My code successfully prints out the first letter. I want to print out the ith character, I'm checking for a space with isblank() with an if statement which if true then I thought would print out the ith character but instead its printing the entire string.

int main(void)
{

    // Prompts and retreves user input.
    printf("Enter Name: ");
    string name = GetString();        


    if (name != NULL)
    {        
        printf("%c", name[0]);                           


        for(int i = 0; i < strlen(name); i++)
        {
                if(isblank(name[i]));
                {
                    printf("%c", name[i]);
                }
        }
        printf("\n");
    }   
}    
4
if(isblank(name[i]));

You put a semicolon on the end. That basically means "if( ) then do nothing." The mistake was the semicolon. By adding it, you closed the if statement code block. The code in the curly braces that follows simply becomes a block of code that is bound by the curly braces for scope purposes, but will execute every time.

Next, realize that when you fix this, you will only print spaces when you really want to print the first letter after the space (the initial).

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

| improve this answer | |
  • Oh my gosh! That was it hahaha. Thank you so much! Yeah I knew it was going to print the space I just hadn't fixed it yet, was trying to solve the other issue first. Added an i + 1 in the bracket of name works beautifully. Thanks again! – RicochetRiot Oct 7 '15 at 18:37

You must log in to answer this question.

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