1

I am trying to receive a word as input from a user and store it into an array which stores each letter as its own element.

basically the same as would be done if I were to initialise

char word[] = "hello";

so I could then use loops to check for certain letters.

I have tried using scanf in various ways with %c and %s specifiers and using a loop to read letter by letter, which did not work.

Everything I have tried so far just stores the full word as the first element in the array or just throws errors.

Thanks for the help!

borgster
  • 11
  • 1
  • 3
    Don't describe your failing code but show it. You've done something wrong, but without seeing your code nobody can tell what youv'e done wrong. – Jabberwocky Jul 17 '21 at 13:23

1 Answers1

1

What you describe cannot cause errors. Rather, you are implementing your plan incorrectly. Try this and you will succeed.

#include<stdio.h>

int main(void)
{
char w[100];
int i;
    printf("Enter the word:");
    scanf(" %s", w);
for(i=0;w[i]!='\0';i++)
    printf("w[%d]=%c\n",i,w[i]);
    return 0;
}
Генс
  • 93
  • 5
  • 2
    `scanf(" %s", &w);` without a width limit like `"%99%"` is worse than [`gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – chux - Reinstate Monica Jul 17 '21 at 13:57
  • There are many ways to protect yourself from an array overflow. In this situation, we do not solve this problem. For the guarantee, I have assigned [100] this is a sufficient guarantee for today. And tomorrow we will solve the following problem. But in general, I agree with you, of course. – Генс Jul 17 '21 at 14:02