I am a coding newbie struggling with initials.c in pset2. I am getting closer thanks to the help of many of you. My code below now capitalizes the first letter of the string and finds every next word in the string and capitalizes them. My problem is I only want to capitalize and print the FIRST LETTER of each word. I do not want to print the whole word. Can anyone help with this?

[hidden for academic honesty]


4 Answers 4



You have one loop (the for loop) that will iterate the string from s[1] till s[length - 1]. And that's perfect.

Then you check if s[i-1] == ' ' , and that's fine too.

Why do you then have this?

while (s[i] != '\0')

That will print every character after the second initial is found in all caps. So if the input is:

"John Smith"

It will print


You only need to iterate through the string once. One iteration == one loop.

  • It worked! I'm now hiding my code but you guys are awesome!
    – TechRav
    Jul 26, 2015 at 11:36
  • you don't need braces around the first printf statement.
  • you already processed s[0], you should start with i set to 1.
  • you're never checking for a space character to print the following character.
  • increment the i on its own line.
  • you need to indent your lasts printf statement well.
  • you need to put some comments.
  • Thanks for your suggestions Kareem. Many I was planning on implementing. I first wanted to get my program working and I then I was going to focus on style by adding comments and making sure my indents are all good. Still haven't got a working program. See my comments below.
    – TechRav
    Jul 24, 2015 at 21:48
  • @TechRav you may update the original post instead!
    – kzidane
    Jul 24, 2015 at 23:02
  • Just changed my code and edited into my original post. Still need some help.
    – TechRav
    Jul 26, 2015 at 4:01

What you are doing right now is just printing the string in caps.

What you need to do is to print the letter as it is unless it comes after space.

There are many ways to achieve that but you will not get away without using the 'if' condition.

You could use a bool to indicate that the char you are iterating over now is a space.

bool space = false;
if(s[i] == ' ') space = true;

if(space == true) or just if(space)

that would mean that the char before was space and now you have to print it in caps.

Or you could check if previous char was a space

if(s[i-1] == ' ')

or you could use the isalpha function

  • "What you need to do is to print the letter as it is unless it comes after space." Actually, this is not true. The assignment is to detect the initials (the first letters of each name/word, capitalize and print them. You should not be printing the other letters. That means the first letter in the name and any letter following a space.
    – Cliff B
    Jul 24, 2015 at 20:03
  • I made the following change to add an IF statement and make it a Do While Loop and still only returns first letter of string in caps. No additional letters. if (s[i-1] == ' ') do { printf("%c", toupper(s[i])); i++; } while (s[i] != '\0'); printf("\n"); return 0; }
    – TechRav
    Jul 24, 2015 at 21:43
  • Just changed my code and edited into my original post. Still need some help.
    – TechRav
    Jul 26, 2015 at 4:01

Well, I'll try to help. 1) Print the first character of the string (capitalized). 2) Iterate from the second element s[1] to the end of the string. 3) If you find a space (ASCII : 32) just print the next element [i+1] capitalized. 4) Continue iterating. Hope it helps. Antonio.

You must log in to answer this question.

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