2

I need to print the initials of a name, like tyler jae woodbury would print TJW, but I can't seem to print the uppercase initials.

This is my code:

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

string get_initials(string name, char initials[]);

int main(void)
{
    // User input
    string name = get_string("Name: ");

    // Gets the users initials
    char initials[10];
    get_initials(name, initials);
    printf("%s\n", initials);
}

string get_initials(string name, char initials[])
{
    int counter = 0;

    for (int i = 0, n = strlen(name); i < n; i++)
    {
        if (name[i] == ' ')
        {
            initials[counter] = name[i+1];
            counter++;
        }
        else if (i == 0)
        {
            initials[counter] = name[i];
            counter++;
        }
    }
    return initials;
}

I know that usually toupper() is used for chars, and the print statement declares a string, but I don't know what to do.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tyler
  • 21
  • 2
  • 2
    [man toupper](https://linux.die.net/man/3/toupper) – Paul R Nov 09 '22 at 21:08
  • 6
    CS50's benighted `string` abomination strikes again. `string` is nothing more than a `typedef` for `char *`. It's literally a pointer to an array of `char`, which you seem to have a decent understanding of... – Andrew Henle Nov 09 '22 at 21:08
  • 2
    `initials[counter] = toupper(name[i+1]);` – Retired Ninja Nov 09 '22 at 21:21
  • You'll need a write a string terminator with `initials[counter] = '\0'` before `return initials;`. Aside: if you don't know how many words will be input a better *bet* than `char initials[10];` might be `char initials[100];`. And/or make a check on `counter`. – Weather Vane Nov 09 '22 at 21:23

1 Answers1

1

The function is incorrect.

For starters in general a string can contain adjacent spaces between words or have trailing adjacent spaces.

Secondly the function does not build a string because it does not append the terminating zero character '\0' to the destination array.

Also the call of strlen is inefficient and redundant.

To convert a symbol to upper case use standard function toupper declared in the header <ctype.h>

Also the function declaration is confusing

string get_initials(string name, char initials[]);

Either use

string get_initials(string name, string initials);

or it will be better to write

char * get_initials( const char *name, char *initials);

The function can be defined the following way as shown in the demonstration program below.

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

char * get_initials( const char *name, char *initials )
{
    const char *blank = " \t";

    char *p = initials;

    while ( name += strspn( name, blank ), *name )
    {
        *p++ = toupper( ( unsigned char )*name );

        name += strcspn( name, blank );
    }

    *p = '\0'; 

    return initials;
}

int main( void ) 
{
    char name[] = " tyler jae woodbury ";
    char initials[10];

    puts( get_initials( name, initials ) );
}

The program output is

TJW
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335