1

I run this program in my terminal and it compiles right, but I don't quite understand why, when I type something, no output is printed and the blinking prompt with the home directory shows up again. I will show you my code to see if you can spot the errors. Thank you.

        #include <stdio.h>
#include <cs50.h>
#include <string.h>
void PrintInitial(string name);
int nextchar;
int j=0;
char initials[15];
char changes[10];
int main (int argc, string argv[])
{
   string s=GetString();
   PrintInitial(s);
}
void PrintInitial(string name)
{
  if (name!=NULL)
  {
  nextchar=1;
   for(int i=0; i>strlen(name); i++)
   {
         if ((nextchar==1)&&(name[i]!=' '))
         {
            if ((name[i]>='a') && (name[i]<='z'))
            {
             changes[j]= (name[i]-('a'-'A'));
             j+=1;
            }
            else
            {
             changes[j]= name[i];
             j+=1;
            }
        }

        nextchar=0;

      if (name[i]==' ')
      {
          nextchar=1;
      }
   }
   printf("%s", changes);
  }
}
1
  • 1
    What do you think happens when i is initially 0 and the continuation condition of the for loop tests whether i > strlen(name)?
    – kzidane
    Mar 22 '15 at 10:51
1

Check the line where you set up the loop.

0

You should indent it better. And check the first for loop. It says for(int i=0; i>strlen(name); i++ you got the i>strlen(name); wrong. It should be int l = strlen(name); i < l; (the definition of the new variable is used so c wont have to call the function again and again and again. So the program should be:

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

void PrintInitial(string name);

int nextchar;
int j = 0;
char initials[15];
char changes[10];

int main (int argc, string argv[])
{
    string s = get_string();
    PrintInitial(s);
}
void PrintInitial(string name)
{
    if (name != NULL)
    {
        nextchar  =1;
        for (int i=0; int l = strlen(name); i < l; i++)
        {
            if ((nextchar == 1) & (name[i] != ' '))
            {
                if ((name[i] >= 'a') & (name[i] <= 'z'))
                {
                    changes[j] = (name[i] - ('a'-'A'));
                    j += 1;
                }
                else
                {
                   changes[j] = name[i];
                   j += 1;
                }
            }
            nextchar = 0;

            if (name[i] == ' ')
            {
                nextchar = 1;
            }
        }
        printf("%s", changes);
    }
}

You must log in to answer this question.

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