1

it's nice to be here!

In initials.c in , I am able to extract and capitalize (if necessary) the first letter of the inputted name string, iterate through the string and extract and capitalize any subsequent initials immediately following a space, however the printed output contains garbled non-ascii characters so I'm clearly doing something wrong..

Here is the relevant trimmed code:

if (name != NULL)
{
    for (int i = 1, n = strlen(name); i < n; i++)
    { 
        if (name[i] == ' ')
        {
            initials[i] = toupper(name[i+1]);
        } 
    }
    printf("%s\n", initials); 
}

And a couple of examples of the output:

jharvard@appliance (~/Dropbox/pset2): ./initials
Robert thomas Bowden
R�8yT�
jharvard@appliance (~/Dropbox/pset2): ./initials
Just another name
J�A�s�
jharvard@appliance (~/Dropbox/pset2): ./initials
a l s
ALS8    r�

I was wondering if the garbage between the initials is being created when I iterate over the unused letters, or if it is a function of not recognising the end of the string correctly..?

I feel like I can nearly grasp what's happening but it's just out of reach..

  • What output do you get? – i_am_david Aug 8 '15 at 19:10
  • It's nice to have you here. Good job explaining exactly what your problem is and what is wrong. Edit your question to include only the relevant code. If there is excess code most probably it will be trimmed by a mod. – ChrisG Aug 8 '15 at 19:12
  • Thanks for the tips, editing now.. – Andrew Smith Aug 8 '15 at 19:43
  • your printing shouldn't be inside the for loop? – Habib ur Rehman Aug 8 '15 at 21:14
  • That doesn't alter the output. – Andrew Smith Aug 8 '15 at 21:33
2

First of all it's not needed to create an array to store the letters you get as initials, as this will require a bit more advanced programing, you can just print the letters to the standard output with printf. For example change this command

initials[i] = toupper(name[i+1]);

to

printf("%c", toupper(name[i+1]));

and after the for loop finishes just print a new line like that:

printf("\n");

That will fix the errors you get.

Now let's get a little more sophisticated to help you see why you are getting these characters. First of all, to store a string, that is an array of characters, you must know the length of the string beforehand. I don't know how you are declaring initials in your program, but I would declare it as the whole length of the name, as the initials are always going to be less than that. Or I would count the number of spaces and make the length the number of spaces + 1. You can do the first like:

char initials[strlen(name)];

The second would be more complex like:

int length = 0;
for (int i = 0, n = strlen(name); i < n; i++)
{
    if (name[i] == ' ')
    {
        length++;
    }
}
char initials[length + 2];

Then it's time to fill it with the initial characters. You are correctly traversing the name string, but you should also keep a record of how many letters you have already put inside initials, as they have to start from the beginning of the array, and each letter be next to another. In your code you have indexes inside your initials that are never assigned values. Also always remember to terminate your strings with a null zero (i.e. \0) so that the program knows where the string ends and doesn't continue to read past the end of the actual string. So the string for Just another name should be ['J', 'A', 'N', '\0'] I would write this code here but I think I have already given you the necessary to keep going on your own. If you need more help let me know in the comments.

| improve this answer | |
  • That's super helpful. I did see somewhere in CS50 the suggestion of outputting directly but I was sidetracked by trying to directly output the 'i'th char, instead of simply using a placeholder and referencing the name variable. – Andrew Smith Aug 8 '15 at 21:46
  • Glad I could help. If you are satisfied with the answer and it solved the problem you had you can mark it as correct by pressing the gray checkmark to the left and vote it up by pressing the up arrow above the checkmark. – ChrisG Aug 8 '15 at 21:48
  • Ha! I got it, thanks! I did have a question about whether I could use toupper inside the printf function but I tried it and lo.. – Andrew Smith Aug 8 '15 at 21:58
  • Of course you can! And as I have it it's wrong! Good catch! :) I'll edit my answer. – ChrisG Aug 8 '15 at 21:59
  • Thanks again. Quick housekeeping question: I edited my op then saw that you had changed it back. I take it I should leave the op untouched and work the answer down the page as has happened here? – Andrew Smith Aug 8 '15 at 22:05
2

It looks to me like the string 'initials' should have a different index variable, say j instead of i. Create and initialize to zero another int called j

replace

initials[i] = toupper(name[i+1]);

with

initials[j] = toupper(name[i+1]);
j++;
| improve this answer | |
  • Great first answer! – i_am_david Aug 8 '15 at 21:10
  • I declared and initialised a global variable j, which has changed the output considerably, but I am still getting junk at the end of my output.. – Andrew Smith Aug 8 '15 at 21:10

You must log in to answer this question.

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