[英]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.