简体   繁体   English

我正在尝试使字符串仅接受字母和空格,但不带数字

[英]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. isalpha使用整数作为参数。 You are giving a char array. 您正在提供一个char数组。

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以让fgets()只接受字母吗? - Can I make fgets() accept only letters? 我正在尝试计算 C 中字符串中的数字 - I'm trying to count the numbers in a string in C 如何只接受C语言中的数字并阻止用户输入字母和特殊字符? - How do I only accept numbers in C and block the user from entering letters and special characters? 我需要一种只接受大写字母的方法 - I need a way to accept UPPERCASE letters only 我正在尝试制作字符串解析器,但出了点问题 - I'm trying to make string parser but something is going wrong 我只想接受奇数 - I want to accept only odd numbers 我怎样才能制作一个只接受数字和字符“。”的字符串在 c 中? - How can i make a string that only accepts numbers and the character “.” in c? 如果我强制在长度为10的字符串中的第一个和最后一个为字母,如何强制用户仅在字符串中插入数字? 示例S2351566A - How to force user to insert only numbers in a string if I force the first and the last to be letters in a 10 length string? Example S2351566A 我正在尝试制作一个 function 将 integer 转换为不返回字符串的字符串。 我该如何解决这个问题? - I'm trying to make a function that converts integer to string which is not returning the string. How can I solve this? 如何确保我的输入字符串只有字符且不超过 10 个字母? - How can I make sure my input string has only characters & is 10 letters or less?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM