[英]Find spaces and alphanumeric characters in a string C Language
Hi i'm trying to build a function in C language that checks if the string contains numbers, upper cases and lower cases and space, if the string contains any other character then those the function return -1.嗨,我正在尝试在 C 语言中构建 function 以检查字符串是否包含数字、大写和小写以及空格,如果字符串包含任何其他字符,则返回 -ZC1C425268E68385D1AB5074F714。
float avg_word_len(char* str)
{
float check;
for (int i = 0; i < strlen(str); i++)
{
if (((str[i] >= '0') && (str[i] <= '9')&&(str[i] >= 'a') && (str[i] <= 'z') && (str[i] == ' ')&& (str[i] >= 'A') && (str[i] <= 'Z')))
check = 1;
else
check = -1;
}
str = '\0';
return check;
that's my code,but the function keep return -1 please help这是我的代码,但 function 保持返回 -1 请帮助
Some of your &&
must replaced by ||
您的一些&&
必须替换为||
because one character is a number OR a lower case OR a space OR an upper case, but it cannot be all these things at a time:因为一个字符是数字或小写字母或空格或大写字母,但不能同时是所有这些东西:
check = 1;
for (int i = 0; i < strlen(str); i++)
{
if (! (((str[i] >= '0') && (str[i] <= '9')) ||
((str[i] >= 'a') && (str[i] <= 'z')) ||
(str[i] == ' ') ||
((str[i] >= 'A') && (str[i] <= 'Z')))) {
check = -1;
break;
}
}
As Weather Vane commented, a lot of those &&
s should be ||
正如Weather Vane评论的那样,很多&&
应该是||
s - additionally, parentheses should surround each range (eg ('0' <= str[i] && str[i] <= '9')
)). s - 此外,括号应围绕每个范围(例如('0' <= str[i] && str[i] <= '9')
))。
To check whether the character is in a range, we use AND (ie the character is above the lower bound AND below the upper bound).要检查字符是否在范围内,我们使用 AND(即字符在下限之上和上限之下)。 To check whether the character is in one of multiple ranges, we use OR (ie the character is in range 1 OR it is in range 2 OR...).要检查字符是否在多个范围之一中,我们使用 OR(即字符在范围 1 中或在范围 2 中或...)。
If we were to only fix that, here's how the if
condition would look:如果我们只解决这个问题,下面是if
条件的样子:
(str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] == ' ') || (str[i] >= 'A' && str[i] <= 'Z')
Having said that, I would suggest using the function isalnum
from ctype.h
in the standard library, which checks if a character is alphanumeric.话虽如此,我建议在标准库中使用来自ctype.h
的 function isalnum
,它检查字符是否为字母数字。 It makes the code much simpler and avoids the assumption that characters are ordered in such a way that all lowercase letters lie between 'a'
and 'z'
(which is true in ASCII - which is what is used in practice - but is not standard).它使代码更简单,并避免假设字符的排序方式是所有小写字母都位于'a'
和'z'
之间(这在 ASCII 中是正确的 - 这是在实践中使用的 - 但不是标准的)。
In addition, I would suggest initializing check
to -1
and breaking early from the loop once you find a non-alphanumeric, non-space character so that a -1
is not later overwritten by a 1
(as would happen in the current version of your code).此外,我建议将check
初始化为-1
并在找到非字母数字、非空格字符后尽早从循环中中断,这样-1
以后不会被1
覆盖(就像在当前版本的你的代码)。
This is what it would look like:这就是它的样子:
float check = -1;
for (int i = 0; i < strlen(str); i++)
{
if (!isalnum(str[i]) && str[i] != ' ') {
check = 1;
break;
}
}
You can use these three function which are countain in the hreader #include <ctype.h>您可以使用这三个 function,它们是在阅读器中计数的#include <ctype.h>
isalpha : Checks if a character is alphabetic (upper or lower case). isalpha :检查字符是否为字母(大写或小写)。
isdigit : Checks if a character is a number. isdigit :检查字符是否为数字。
isblank : Checks whether a character is blank or not. isblank :检查字符是否为空白。
#include <ctype.h>
#include <stdio.h>
float avg_word_len(char* string)
{int check=-1;
for(int i=0;string[i]!='\0';i++)
{
if(isalpha(string[i])||isdigit(string[i])||isblank(string[i]))
{
check=1;
}
}
return check;
}
int main()
{
char string[150];
printf("Give me a text :");
scanf("%s[^\n]",string);
printf("\n%.f\n",avg_word_len(string));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.