0

I know theirs a billion ways to implement a solution , heres what i came up with and it passed all the tests. What do you guys think

int main(void) {
    string name = get_string();
    void setInitials(string name);
    setInitials(name);
}

void setInitials(string name) {
    printf("%c",toupper(name[0]));
    for (int i = 0; i < strlen(name);i++) {
        if(isspace(name[i])) {
            i++;
            printf("%c",toupper(name[i]));
        }
    }
    printf("\n");
}
0

In main(), in the third line, you have

void setInitials(string name);

which, if I guess correctly, is the prototype for the function setInitials(), following main(). If so, you should put the prototype outside any other function, and at the top of your source file, right after the #includes.

In your set initials, you have the loop

for (int i = 0; i < strlen(name);i++)

which at the start of every loop, computes the length of the string name, which doesn't change inside the loop. Instead, consider computing the length only on the first iteration of the loop, to speed it up, like so:

for (int i = 0, length = strlen(name); i < length; i++)

In your setInitials(), inside the if(isspace(name[i])), you have

i++;

It's generally considered bad practice to increment inside a for loop, the variable that controls the for loop. Consider using a while loop instead. for loops are usually used when you know the exact number of times the loop will run, which in your case is not true, since you increment i based on some condition, inside the loop.


Bare in mind that your question in primarily opinion based and such questions are not so welcome in SE. Nevertheless I tried to answer your question as objectively as I could based on best practices I use myself.

Happy Coding!

2
  • 1
    Thanks for the advice , so going forward do not post these opinion based questions? For me its more so getting feedback for better and cleaner code vs asking for questions revolving the psets. Jun 23 '17 at 6:59
  • 1
    Here's what you should do when someone answers your question. You may find the SE code review of interest. Jun 25 '17 at 23:13

You must log in to answer this question.

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