简体   繁体   中英

I'm trying to make a string accept only letters and space BUT NO NUMBERS

This is what I theorized it should be but it seemed like it doesn't work. HELP PLEAZEE

    int main()
    {
       char input[50];
       int i;

    do{
      printf("ENTER A CHARACTER:");
      scanf("%s",&input);

    if(isalpha(input)!=0){
       printf("YOU INPUTTED A CHARACTER");
       i++;
    }else{
       printf("INVALID INPUT\n");
    }

    }while(i!=1);

    }

isalpha takes an integer as an argument. You are giving a char array.

You should loop for the number of characters given in input[], if you want more than one character (hard to tell from this code). This exits if you give exactly one character but keeps going if you give more than one:

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

int main()
{
    char input[50];
    int i = 0, j;
    size_t len = 0;

    do
    {
        printf("ENTER A CHARACTER:");
        scanf("%s",&input);

        len = strlen(input);

        for(j = 0; j < len; j++) {
            if(isalpha(input[j]))
            {
                i++;
                printf("YOU INPUTTED A CHARACTER %d\n", i);
            } 
            else 
            {
                printf("INVALID INPUT\n");
                break;
            }
        }
    } while(i!=1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM