I'm struggling with printing the initials after the first name, I'm following the less comfortable specs, my pseudocode;

//prompt user for name and store as string

//convert strlen to an integer

//print first character using toupper for lower case inputs

//using a for loop, print the char directly after a space, again using toupper

My code prints the first character just fine but no following characters, I can assume that there are no extra spaces, the for loop is as follows;

    for ( int j = 0; j <= i; j++ )

        if ( (char)s[j] == ' ')
        printf("%c", toupper( s[j + 1] ) );

It compiles just fine so it's not a syntax error, any help would be greatly appreciated as I'm getting to the stage of just trying different wording which doesn't really help me much as far as learning goes.


  • How is i being declared and computed? Are there any if clauses before this loop that would possibly skip it? – DinoCoderSaurus Feb 13 '17 at 18:50
  • You were absolutely spot on! I had declared i unknowingly after setting it to strlen as part of my first initial as follows; printf("%c", toupper( s[ i = 0 ]) ); when I can just use s[0]. I didn't realise this would over ride the pre assigned value for i. Thank you very much :) – Thomas Forrest Feb 13 '17 at 18:58
  • Great! Suggest you make that an answer, instead of (or in addition to) a comment. That might help others facing a similar problem. Then, in 2 days time, you could accept it as the answer. – DinoCoderSaurus Feb 13 '17 at 20:19

As DinoCoderSaurus helpfully pointed out in the comments, I was trying to use the integer i believing it to have the value of the string length, but I had actually set it to 0 in an earlier line of code, using the debug tool properly would have shown me this as I went through line by line.

You must log in to answer this question.

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