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

int main()
{
string userName;

do {
  userName = GetString();
} while (userName == NULL);

if (!isspace(userName[0]))
    {
      putchar (toupper(userName[0]));
    }

for (int i = 0, n = strlen(userName) - 1; i < n; i++)
{
    if (userName[i] == ' ')
    {

        if (userName[i + 1] != ' ')
        {

          for (int j = i + 1; j < i + 2; j++)
          {
            putchar (toupper(userName[j]));
          }

        }

    }

}

printf("\n");

}

Right now i checked back on my initials (more) Code and realized i could make it a lot more effective and cleaner now, but still i somehow wasn´t able to use the ctype library Syntax very well i fell, and only have toupper working so far. Any suggestions for better Writing here would be appreciated.

0

So far, your code actually looks pretty clean. However, there is one thing you can do to make it look even cleaner: You could add comments to your code.

Even though it's likely that most of the people seeing your program will understand it, it is still possible that someone who doesn't know how to code will stumble upon your program. If this happens, how will that person know what the program is doing? Comments help others understand how your program works. Comments can also help you follow along with your own code. Comments can also make your code look cleaner.

As for effectiveness, might I ask why you have the for-loop for for (int j = i + 1; j < i + 2; j++)? Think about it - how many times will this iterate? This only iterates once! A for-loop is best used when you have to iterate over something multiple times. Since it only iterates once, why don't you just putchar(toupper(userName[i + 1])? It'll do the same thing in less code.

Also, you can combine your if statements into:

if (userName[i] == ' ' && userName[i + 1] != ' ')

If this answers your question, please hit the green checkmark! If you still have issues/questions, comment below. :-)

4
  • Thank you for your hints to clean this up :) I removed the redundant j loop and also merged the if statements together! The original idea of the j loop was to adress the Letter after the space, but now i realized that the if statements do this completely themselves. I am not sure for what to add another if/else loop(?) since i covered already the possibility of a Milo Banana Spacebar Rampage in the [0] Space and it seems as if the Programm also picks up all the Initials easily. Mar 29 '17 at 16:58
  • I am still Confused :/ Since the check50 says BOB is producing the correct output. If i type in BOB or Bob there is only a B which is exactly what i would expect. I don´t get the issue you try to point out. Mar 29 '17 at 21:08
  • I apologize, my mistake. You were correct! :-) Shows how much you learn on SE. Forget all I said. :-) Mar 29 '17 at 21:53
  • Not all of it ;D Just the last Part :P And yes, i will visit SE more often now ;) Mar 30 '17 at 9:13

You must log in to answer this question.

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