Arrays 检查字符串是否仅为字母和空格
我编写了这个简单的代码来检查字符串是否仅为字母和空格Arrays 检查字符串是否仅为字母和空格,arrays,c,function,char,c-strings,Arrays,C,Function,Char,C Strings,我编写了这个简单的代码来检查字符串是否仅为字母和空格 #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<string.h> #define N 100 int checkString(char str1[]); void main() { char str1[N]; scanf("%s", str1);
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define N 100
int checkString(char str1[]);
void main()
{
char str1[N];
scanf("%s", str1);
printf("%d",checkString(str1));
getch();
}
int checkString(char str1[])
{
int i, x=0, p;
p=strlen(str1);
for (i = 0; i < p ; i++)
{
if ((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z') || (str1[i] == ' '))
{
continue;
}
else{ return 0; }
}
return 1;
}
但如果我在空格后键入任何内容,它将返回1
,如下所示:
hello 1220//返回1
blabla11sdws//返回1
有人能告诉我为什么吗?你忘了数字
int checkString(char str1[]) {
int i, x=0, p;
p=strlen(str1);
for (i = 0; i < p ; i++)
if ((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z') || (str1[i] == ' ') || (str1[i] >= '0' && str1[i] <= '9')) {
continue;
} else return 0;
return 1;
}
int校验字符串(char str1[]{
int i,x=0,p;
p=strlen(str1);
对于(i=0;i例如,如果((str1[i]>='a'&&str1[i]='a'&&str1[i]='0'&&str1[i]使用头
中声明的标准C函数isalpha
和isblank
,则函数可以编写得更简单、更正确
#include <ctype.h>
//...
int checkString( const char s[] )
{
unsigned char c;
while ( ( c = *s ) && ( isalpha( c ) || isblank( c ) ) ) ++s;
return *s == '\0';
}
fgets( str1, sizeof( str1 ), stdin );
size_t n = strlen( str1 );
if ( n != 0 && str1[n-1] == '\n' ) str1[n-1] = '\0';
考虑到函数包含新行字符。因此,在输入字符串后,应删除此字符。例如
#include <ctype.h>
//...
int checkString( const char s[] )
{
unsigned char c;
while ( ( c = *s ) && ( isalpha( c ) || isblank( c ) ) ) ++s;
return *s == '\0';
}
fgets( str1, sizeof( str1 ), stdin );
size_t n = strlen( str1 );
if ( n != 0 && str1[n-1] == '\n' ) str1[n-1] = '\0';
发生这种情况是因为您正在使用scanf(%s,&str)进行输入。通过这种方式,只在存储空格之前输入字符\n或其他空白字符。因此,当您输入空格时,输入只存储到空格
例如,输入helloo1234
您的str只存储helloo,而1234仍保留在缓冲区中。请尝试使用getchar()
#包括
#包括
#包括
#包括
#包括
#定义N 100
int校验字符串(char str1[]);
void main()
{
字符str1[N];
int i=0;
而(1)
{
str1[i++]=getchar();
如果(str1[i-1]='\n')中断;
}
printf(“%d”,校验字符串(str1));
getch();
}
int校验字符串(字符str1[]
{
int i,x=0,p;
p=strlen(str1);
对于(i=0;i如果((str1[i]>='a'&&str1[i]='a'&&str1[i]您的输入字符串有问题
scanf()
因此,当您以hello 1234的形式输入时,它会检查您的实际输入是否为hello。请通过打印您正在输入的内容(即打印str1
)来检查此项。然后您将了解此代码中的错误
你可以使用gets
或fgets
来解决这个问题。当你使用scanf(“%s”,str1);
时,你输入hello 112
,str1得到的是hello
。因此你可以使用fgets(str1,N,stdin);
来获取字符串。我认为它会起作用。如果你打印回刚才扫描的字符串()ed您会注意到,它只获取所有输入的第一部分。即,包括空白在内的空白之后的任何内容都将被忽略。
可以使用getch()(windows)或getchar()(linux)获取每个字符输入,并在有“\n”(换行符)时终止
资料来源:
#包括
#包括
#包括
#包括
#定义N 100
int校验字符串(char str1[]);
void main()
{
int i=0;
INTC;
字符str1[N];
memset(str1,0,sizeof(str1));
做{
c=getchar();
str1[i++]=c;
}而((c!='\n')&&(i<(n-1));/(i
您没有抓住问题的关键。输入的数字正是OP希望函数为字符串(如“hello 123”)返回0的原因。问题是读取字符串的方法,它会在第一个空格处停止。@WhozCraig buh…是的,我当时肯定错过了它。你知道%s
只读取第一个字符字符串直到第一个空格字符,对吗?因此,如果你输入“hello 123”
,那么str
将包含“hello”
,这符合您对所有字母或空格的测试。也许稍微修改一下尾随的换行符可能更符合您的要求。不,我不知道,谢谢您告诉我me@WhozCraig详细信息:“%s”
首先跳过前导空格,然后读取(和存储)第一个字符字符串,直到第一个空格char
。