简体   繁体   English

在字符串中查找空格和字母数字字符 C 语言

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

相关问题 在字符串中查找空格和字母数字字符 - Find spaces and alphanumeric characters in a string 从C中的字符串中删除非字母数字字符 - Removing non alphanumeric characters from a string in C 如何用C语言读取包含空格的字符串 - How To Read String that contains Spaces, in C language 如何从带有空格的字符串中连接 C 中的字符? - How to concatenate characters in C from a string with spaces? 字符串/正则表达式字符&#39;[&#39;,&#39;]&#39;,&#39;{&#39;,&#39;}&#39;用C语言在大型机TN3270(代码页1047,1147,500,249)上替换为空格 - String/Regular expression characters '[', ']', '{', '}' replaced by spaces on Mainframe TN3270 (with code page 1047,1147,500,249) in C language 为什么 C 语言的字符串开头有 3 个不需要的字符? - why are there 3 undesired characters in the beginning of the string in C language? 如何在C语言中交替两个字符串字符 - how to alternating two string characters in c language 如何遍历2个字符的字符串的字母数字字符 - How to iterate through alphanumeric characters for a string of 2 characters 将带有字符、空格和整数的字符串分隔成字符串和 C 中的 integer - Separate a string with characters, spaces and integers into string and integer in C C语言从给定字符串中删除前面的空格和制表符 - Remove preceding spaces and tabs from a given string in C language
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM