0

This is my code for Problem Set-2 initials.c :

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

int main(void)
{
    int i,j=0;
    string name;
   char initials[80];
   name=GetString();
   initials[j++]=toupper(name[0]);
    for(i=0;i<strlen(name);i++)
{
    if(!isalpha(name[i]))
      initials[j++]=toupper(name[i+1]);
}
initials[strlen(initials)-1]='\0';
printf("%s\n",initials);
}

When checking the output through terminal this is what happens :

~/workspace/pset2 $ ./initials
milo banana
MB
~/workspace/pset2 $ ./initials
ROB
R
~/workspace/pset2 $ check50 2015.fall.pset2.initials initials.c
:) initials.c exists
:) initials.c compiles
:( outputs "MB" for "Milo Banana"
   \ expected output, but not "MBw\n"
:( outputs "MB" for "milo banana"
   \ expected output, but not "MBw\n"
:) outputs "RTB" for "Robert Thomas Bowden"
:( outputs "R" for "ROB"
   \ expected output, but not "Rÿw\n"
:) outputs "RTB" for "Robert thomas Bowden"

I seriously didn't understand what's going wrong with check50 when I am getting appropriate output in the same terminal. Please help...

0

I ran your code in the IDE and got variable and unpredictable results, especially when I varied the input. The problem is amplified when this code is run on a system with heavier workload and a lot of memory reuse.

The problem is that this code is printing characters beyond what you want printed. The root cause is this:

initials[strlen(initials)-1]='\0';

You are using strlen() to place the end of string marker. The problem there is that strlen() depends on the end of string marker to determine the string length. When this line executes, it's going to look for a byte that is all binary zeros. If the memory in the string initials contains random garbage data (it isn't initialized to zero by default), strlen() is going to search for the end of string marker and will hunt for the random zero byte. So, the end of string marker is placed unpredictably wherever it finds a byte of all 0's. It's kind of like saying "place an x where you find the first x."

Instead, you've tracked the location of where the \0 marker should go with the variable j. Why not use it?

Also, there's a lesser issue with the string name. Remember that when you declare a string, you set it's size at that point and that size can't be changed. By simply declaring it with string name;, it has a size of 1 - the end of string marker. When you load it with an input string using GetString(), it will overwrite adjacent memory. As it is, the code may or may not work correctly, even with the same data input. They should be combined into one statement as string name = GetString(); to declare it, set the size and initialize it in one statement.

btw, what would happen if the input string had more than 80 words? Could you rewrite this so that it doesn't matter how long the input is? Think about it.

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

2
  • Practically, I thought names wouldn't be longer than 80 words, so I just randomly picked that number. I didn't get what you meant exactly. I am new to coding. So, I don't know how to capture input of any length without asking the user. Please help me. And Thanks a lot for the solution. Jun 26 '16 at 17:21
  • You should pick up on all this as you go. This is only the 2nd pset, and only the first part of it. Enjoy!
    – Cliff B
    Jun 26 '16 at 17:32

You must log in to answer this question.

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