0

My solution gives your initials from strings with and without leading spaces between the letters. It also starts a newline at the end. Please advise, I am pretty clueless as to why it won't be accepted. I'll post code here too to help troubleshooting. Thanks!

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

int main (void)
{
   int n;
   int i=0; 
   string name = get_string();                    //get name to extract initials from
   n=strlen(name);

   printf("%c", toupper(name[i]));                 // Print first line because of logil hole when there isn't a space before

   while (i<n)                                    //Loop while position is less than name string length
   {
      if(isalpha(name[i]) && isspace(name[i-1]))  //Print Character in uppercase if itis a lettter and there was a space in the spot before (aka first letter of name)
      {
         printf("%c", toupper(name[i]));
      };
   i++;
   }
   printf("\n");
3
  • I can't figure out formatting for the life of me, but my last post magically formatted after waiting, can someone advise? Thanks for your help.
    – Mitch Beck
    Mar 17 '17 at 0:07
  • You can edit this post, select the code and click the {}. That should format it correctly. When you are creating a question, select the code after you paste and click the {}. Your prior post did not "magically" format. Look at it again and you will see that it was edited by Cliff B. Mar 17 '17 at 2:56
  • Thanks Dino, I knew it was too good to be true!
    – Mitch Beck
    Mar 17 '17 at 15:02
1

Your program does not "behave" as per the supplied examples. When you try this example (Italics indicate what some user has typed):

$ ./initials
Zamyla Chan
ZC

do you get exactly the same output?

As per your comment, your output is:

$ ./initials
Enter your name:
Zamyla Chan
ZC

Particularly in the early pset's, check50 expects strict adherence to the specification. Just like a boss or a customer might.

If you are doing "more" version, you'll likely run into trouble here printf("%c", toupper(name[i])); (the first one). When it executes that line, i = 0. If name[i] is space, it will print space. You need some kind of loop to find the first non-space in the name. It doesn't look like you will have trouble with trailing space.

5
  • I get the following: ~/workspace/pset2/initials/ $ ./initials Enter your name: Zamyla Chan ZC ~/workspace/pset2/initials/ $
    – Mitch Beck
    Mar 17 '17 at 3:15
  • Additional info added to answer. +1 for formatting! Mar 17 '17 at 12:04
  • I am now down to needing to get rid of leading and trailing spaces, which I don't understand because I check it with 'isalpha', which i would think would eliminate letting spaces through. Thank you for any clues.
    – Mitch Beck
    Mar 17 '17 at 15:52
  • Additional info added to answer. Mar 17 '17 at 16:22
  • I switched to using the 'less' grader and it passed. Thanks everyone, esp dino for your help.
    – Mitch Beck
    Mar 17 '17 at 17:05

You must log in to answer this question.

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