2

How to set input string array to accept only specific letters from STDIN??

char arr[testcases][100];
    for(i=0;i<testcases;i++){
    scanf("%99s",&arr[i]);
}

I am going to store only letters a,b,c,d in my array. How can I restrict other letters from getting stored in my array?

Codex
  • 1,153
  • 1
  • 20
  • 31

1 Answers1

2

You can use this:

 scanf("%99[a-d]", arr);

The return value will be 1 if any initial part of the string matches; input will stop at the first non-matching character.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084