0

So I originally started tackling this problem by making arrays for the intials, iterating through the name from get_string looking for spaces and then adding name[i+1] into the initials array, finally itertating again through the initials array and printing each letter. The final code looked like this:

int c = 0;
int spaces = 0;

int main(void)
{
    string name = get_string();
    int n = strlen(name);


    char initials[10];

    // first letter is always going to be the first initial
    initials[0] = name[0];

    // count through letters looking for spaces + add the first letter after a 
    // space to the initials array
    for (int j = 0; j < n; j++)
    {
        if (name[j] == 32)
        {
            c += 1;
            initials[c] += name[j+1];
        }
    }    

    // print out initials
    for (int k = 0; k <= c; k++)
    {
        printf("%c", toupper(initials[k]));
    }    

   printf("\n"); 
}

However I was having problems with the size of the initials array - a lot of numbers other than 10 resulted in unusual characters being returned and I didn't like the fact that '10' just seemed an arbitrary number that happened to work. The more I looked at it the more I thought I was making it more complicated than it needed to be and I now have this version which does everything that the question asks, and it passes the CS50 checks, but it just looks so wrong and feels like I have cut a load of corners..... Is it wrong?

 int main(void)
{
    string name = get_string();
    int n = strlen(name);

   // prints the first letter
    printf("%c", toupper (name[0]));

    // iterate through 'name' looking for spaces + print the first 
    // letter after a space
    for (int j = 0; j < n; j++)
    {
        if (name[j] == ' ')
        {
            printf("%c", toupper (name[j+1]));
        }
    } 

   printf("\n"); 
}
1

The first version has a couple of issues. First, it arbitrarily limited the number of initials that could be handled by using an array to store them for later printing. The second version eliminated that problem by simply printing then as it goes - a much better and simpler algorithm.

Second, and probably why it felt wrong, is the i[] array. It was never initialized and there is nothing in the code to set the end of string marker. However, it does use c to determine the number of chars set.

Finally, some coding issues. Look at the following:

        c += 1;
        initials[c] += name[j+1];

FYI, while it's correct, if you want to increment c by using a coding shortcut, wouldn't it look cleaner to use c++; to replace c = c + 1;?

Next, setting the value of initials with += is a problem. Since initials was never initialized, it could contain any garbage data. Using += adds name[j+1] to that garbage value. A simple = assignment should have been used.

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

| improve this answer | |

You must log in to answer this question.

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