0

I want to create a simple C program to allow user to key in their name and print out their name(e.g. John Mike). But i also want the program to disallow the user to key in name with digits value within the name(e.g. John34 Mike).

I know how to use scanf("%[^\n]",&name); but with that user can accidentally enter digits into input name. I want to disallow user to input alphabets with digits value. Is there any way to solve this?

MIRMIX
  • 1,052
  • 2
  • 14
  • 40
  • There is no way in standard C to filter the input key-for-key, so if you want to do that you need to use some platform-specific code (possibly wrapped in some library). You can check after the input is done, but not stop the digits from being entered. – unwind Nov 03 '17 at 13:23
  • Thank you for the advice – JUZ Aviewer Nov 09 '17 at 07:46

2 Answers2

1

How to allow user to input alphabets with space for name( e.g. John Mike) but disallow user to input alphabets with digits value?

Read in the entire line of input into a string and then process the string.


Assess the string for legitimate characters and patterns.

Important to be generous in what is valid and consider various culture issues. Name validity testing is a sensitive issue. Easy to get negative feed attempting to discuss it. Also may be considered not constructive.

Use isalpha() for first level check and then check for other valid characters insuring at least one alpha.

isalpha() is locale sensitive and offers some level of internationalization.

#include <ctype.h>
#include <stdbool.h>
bool name_test1(const char *s) {
  const char *between_char = "-'";  // Allow O'Doul, Flo-Jo.  Adjust as needed
  bool valid = false;
  while (*s) {
    if (isalpha((unsigned char ) *s)) {
      valid = true;
    } else if (*s == ' ' && valid) { // or  isspace((unsigned char) *s) for any white-space
      valid = false; // valid char must follow
    } else if (strchr(between_char, *s) != NULL && valid) {
      valid = false; // valid char must follow
    } else {
      return false;
    }
    s++;
  }
  return valid;
}

The && valid in *s == ' ' && valid insures a leading space is not valid.

Example:

void ntest(const char *s) {
  printf("%d <%s>\n", name_test1(s), s);
}

int main(void) {
  ntest("John Mike");
  ntest("John34 Mike");
  ntest("John Mike ");
  ntest(" John Mike");
  ntest("Flo-Jo");
  ntest("O'Doul");

  char name[100];
  while (fgets(name, sizeof name, stdin)) {
    name[strcspn(name, "\n")] = '\0';  // lop off potential trailing \n
    if (name_test1(name)) puts("Acceptable name");
    else puts("Unacceptable name");
  }
}

1 <John Mike>
0 <John34 Mike>
0 <John Mike >
0 < John Mike>
1 <Flo-Jo>
1 <O'Doul>
...

Best to not use scanf("%[^\n]",&name); it has problems: It reads almost a line, the '\n' is not read. A line of only "\n" is a problem. It has no protection against over running the buffer.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Get the input using getline function. And then check the string by using isdigit function to allow or disallow the input string.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char *argv[])
       {
           FILE *stream;
           char *line = NULL;
           size_t len = 80;
           ssize_t nread;



           nread = getline(&line, &len, stdin);
           printf("Retrieved line %s", line);
       for(int i=0;line[i]!='\0';i++)
        if(isdigit(line[i])){
            printf("Disallow\n");
                return 0;}
       printf("Allow\n");
       return 0;    

       }
MIRMIX
  • 1,052
  • 2
  • 14
  • 40