-1

I am trying to create a program that take the input from the user and prints the first character of each word but every time I try to Here is my code.

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

int main(void)
{
    char leng[100];
    int len;
    scanf("%s", &leng[100]);
    len = strlen(&leng[100]);
    char name[len];
    //checking if at end or not
    while (name[len] != '\0')
    {
        if (name[len] == ' ')
            printf("%c", name[len + 1]);
        len++;
    }
}

Every time I give a name it shows an error something like:

index 3 out of bounds for type 'char [len]'
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Read the code you posted more closely. Where do you put the input you get with `scanf()`? Where are you trying to read the initials from? (The way you're getting your input is incorrect, but that's a different issue entirely.) You should ask your instructor or teaching assistant for some extra help. – Ken White Oct 05 '16 at 23:53
  • `scanf("%s", &leng[100]);` : `leng[100]` is next of last element. should be `&leng[0]` or just `leng`. Also `%s` doesn't include white-spaces. So `%s` change to `%99[^\n]` – BLUEPIXY Oct 06 '16 at 00:10
  • `char name[len];` --> `char name[len+1];` then `strcpy(name, leng);` Also first character of name isn't print. – BLUEPIXY Oct 06 '16 at 00:12

2 Answers2

2

These two lines are incorrect:

scanf("%s", &leng[100]);
len = strlen(&leng[100]);

If you translate these into English, their meanings as written are:

  • Scan a string to the memory at the address of 101st element of the leng array.
  • Get the length of the string that starts at the address of the 101st element of the leng array.

The array index is out of bounds because leng[100] is past the end of the array. Remember that a 100 element array goes from 0 to 99, not from 1 to 100!

You want to be scanning into the base address of the array, and passing the base address of the array into strlen(). I'll leave the syntax for you to figure out from your textbook.

And by the way, you also have a problem in your code because you're reading your data into an array named leng, but your loop is working with an array named len. There are at least two additional problems in your code, but I'll leave them for you to debug.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
0

There are a few things to consider with your code. As @richardschwartz already mentioned, you are not referencing your char arrays correctly. you have:

scanf("%s", &leng[100]);
len = strlen(&leng[100]);

You may want the following instead:

scanf("%s", leng);
len = strlen(leng);

Also, keep in mind that scanf with the %s flag will stop reading input once white-space is detected. For example, if you input "hello world",

scanf("%s", leng);

will only catch the characters "hello". To get around this, you could loop scanf to read multiple words and return the first character of each word as you desire.

Lastly, scanf is not advised for beginners though. See paxdiablo's excellent reason regarding lack of overflow protection, here: https://stackoverflow.com/a/1248017/6870832

Community
  • 1
  • 1
phreaknik
  • 615
  • 5
  • 12