0

I feel like if I don't seek help from this forum I will end up finishing my CS50 course in 2020 :'(

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

void initials (string a);

int main (void)
{

    string name = get_string ();
    if (name != NULL)
    {
    //prints first letter in capital
    printf("%c", toupper(name [0]));

    //prints rest of the initials
    initials (name); 
    printf("\n");
    }

}

void initials (string a)
{
    int n=0;
    int l=strlen(a);
    for (int i=0; i<l; i++) 
    {
        do
        {
        n++;
        }
        while (a[n]!=32 && n<l);
        printf ("%c", toupper(a[n+1]));
    }
}

My initials.c programme seems to run fine when I test it but sometimes it prints an exclamation mark after the initials (What's going on?).

Also, check50 displays the following response:

:) initials.c exists :) initials.c compiles :( outputs "MB" for "Milo Banana" \ expected output, but not "MB\u0000\u0000\u0000\u0000\u0000\u0000..." :( outputs "MB" for "milo banana" \ expected output, but not "MB\u0000\u0000\u0000\u0000\u0000\u0000..." :( outputs "RTB" for "Robert Thomas Bowden" \ expected output, but not "RTB\u0000\u0000\u0000á\u000f\u0002\u000..." :( outputs "R" for "ROB" \ expected output, but not "R\u0000\u0000\u0000\n" :( outputs "RTB" for "Robert thomas Bowden" \ expected output, but not "RTB\u0000\u0000\u0000á\u000f\u0002\u000..."

I feel like my code is a mess! Help!

(Also if any of you lives in London and am happy to meet up and discuss, please let me know! Learning to code by yourself is too hard :'( )

2 Answers 2

1

It is possible to do this with one loop and index. (I'm assuming this is the less comfortable instructions, because the more comfortable asks you to think about extra space and spaces before the first word of the name.)

Think about starting with the first character and looping until you reach the length of the string. Your for loop does that. Now think about what each character will mean as you read it. If it is a space, you have a break between names. If you have something else, you have to know whether you have just encountered a break or if you are reading the remaining characters of the current name. Your final answer needs to have at least one if statement inside the loop.

An important lesson when working with arrays is to check the bounds before you access anything. You check the character with a[n] before you check the range with n < l. In any && or || statement, the tests are evaluated left to right (and eventually you will learn that C stops evaluating once it knows that the expression must be true or false).

// get the length of the string
// print first character.
// loop from the second character to the length of the string
// is this character a space? 
// If yes, then don't print anything but you do know that the next non-space is the start of a new part of the name.
// If no, then print it if the first character, otherwise do not print anything
//
// When the loop ends, print a final newline.
2
  • Thank you for the help! After some twists to my code it worked finally :) Commented May 29, 2017 at 15:35
  • Thank you for the help! After some twists to my code it worked finally :) Commented May 29, 2017 at 15:35
1

The code you have will throw a seg fault and print garbage values. Notice you have two loops. The 'do while' nested within the 'for' loop. This will cause 'n' to become much larger than the length of 'a'.

If the name input is 'Joe Bloggs' this will mean 'l' will be 10. The 'for' loop will run while i is less than 10. The while loop will run until it hits a space for each iteration of the for loop. On your last print statement you will also seg fault as you add +1 to the length of 'a'.

Put a print statement after 'n++' and you will see the number of loop iterations.

1
  • Thank you for the help! After some twists to my code it worked finally :) Commented May 29, 2017 at 15:35

You must log in to answer this question.

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