0

I seem to be getting the correct output when my string is 10 characters or less, but once that character number increases upon input, I compile the program and the loop seems to shut down and output only the correct initials for up to 10 characters. (and on a separate note: I'm not able to add a jpg or tiff image to this post. any hints?)

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

int main (int argc, char *argv[])
{

    char *s = GetString();
    int i = 0;
    printf("%c", toupper(s[0]));
    int n = strlen(argv[i]); 

    while (i < n)
    {
        i++;

        if (s[i] == ' ')
        {
            printf("%c", toupper(s[i+1]));
        }

    }   

    printf("\n");
}
0

Per the specs, the initials program shouldn't take any command line arguments. It should work like your previous programs and ask for a string using the GetString function.

The problem are below: The statement int n = strlen(argv[i]); is outside of the loop. So the value for i is initially set to 0 and even if the value for i increments, the value of n never does. Its just set once and thats it. Try including the int n = strlen(argv[i]); statement inside the loop.

But again at some point your program might cause problems. As the value for i will keep incrementing, at some point you'll access, lets say the 9th argument in the command line. No one has name that long. so that'll cause you problems. Just don't do any command line argument inputs. Set the value of n to strlen(s), which is the whole name's length, and do the printing.

If thats answers your question, please consider checking this as the answer. Hope that helped! :-)

4
  • Thank you. I was able to correct this code and keep the command line argument. Turns out the int n = strlen(argv[i]) should have been int s = strlen(s) to make this work. – Karen Safrit Jun 15 '16 at 22:38
  • Good to know you fixed that. Your approach was a bit different and glad it worked! Happy coding! 😊 – yousafe007 Jun 15 '16 at 22:40
  • "Try including the int n = strlen(argv[i]); statement inside the loop" amazing!! – MARS Jun 16 '16 at 20:03
  • Hi5 mate! @MARS 😎 – yousafe007 Jun 16 '16 at 20:06

You must log in to answer this question.

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