简体   繁体   中英

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.

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

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 || s - additionally, parentheses should surround each range (eg ('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). 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...).

If we were to only fix that, here's how the if condition would look:

(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. 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).

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).

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>

isalpha : Checks if a character is alphabetic (upper or lower case).

isdigit : Checks if a character is a number.

isblank : Checks whether a character is blank or not.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM