[英]checking if character is upper or lower case in alphanumeric
I have this C code. 我有这个C代码。 If I input a LOL123 it should display that it is uppercase.
如果输入LOL123,它应该显示为大写。 And lol123 it is in lowercase.
而lol123是小写字母。 How do I use isalpha in excluding non-numerical input when checking isupper or is lower?
检查isupper或更低时,如何在排除非数字输入时使用isalpha?
#include <stdio.h>
#define SIZE 6
char input[50];
int my_isupper(char string[]);
int main(){
char input[] = "LOL123";
int m;
m= isupper(input);
if( m==1){
printf("%s is all uppercase.\n", input);
}else
printf("%s is not all uppercase.\n", input);
return 0;
}
int my_isupper(char string[]){
int a,d;
for (a=0; a<SIZE); a++){
d= isupper(string[a]) ;
}
if(d != 0)
d=1;
return d;
}
For upper-case function just loop trough the string and if a lowercase character is encountred you return false
like value. 对于大写功能,只需循环遍历字符串即可;如果遇到小写字符,则返回
false
如value。 And don't use standard library functions names to name your own functions. 并且不要使用标准库函数名称来命名您自己的函数。 Use
isUpperCase
instead. 请改用
isUpperCase
。
Live Demo: https://eval.in/93429 现场演示: https : //eval.in/93429
#include <stdio.h>
#include <string.h>
int isUpperCase(const char *inputString);
int main(void)
{
char inputString1[] = "LOL123";
char inputString2[] = "lol123";
printf("%s is %s\n", inputString1, isUpperCase(inputString1)?"upper-case":"not upper-case");
printf("%s is %s\n", inputString2, isUpperCase(inputString2)?"lower-case":"not upper-case");
return 0;
}
int isUpperCase(const char *inputString)
{
int i;
int len = strlen(inputString);
for (i = 0; i < len; i++) {
if (inputString[i] >= 'a' && inputString[i] <= 'z') {
return 0;
}
}
return 1;
}
int my_isalpha_lower(int c) {
return ((c >= 'a' && c <= 'z')); }
int my_isalpha_upper(int c) {
return ((c >= 'A' && c <= 'Z')); }
int isdigit(int c) {
return (c >= '0' && c <= '9'); }
while (*s) {
if (!is_digit(*s) && !my_isalpha_lower(*s))
{
//isnot lower but is alpha
}
else if (!is_digit(*s) && !my_alpha_upper(*s))
{
//is not upper but is alpha
}
s++;
}
char c = ...;
if (isalpha(c))
{
// do stuff if it's alpha
} else {
// do stuff when not alpha
}
You have a lot to learn, besides using a name of a standard function your design also is completely flawed. 您需要学习很多东西,除了使用标准功能的名称之外,您的设计也完全有缺陷。 You only memorize the case of the last character that you encounter in your
for
loop, so the result that you return is not at all what you think. 您只记住在
for
循环中遇到的最后一个字符的大小写,因此返回的结果根本不符合您的想法。
Some more observations: 其他一些观察:
isupper
to be a logical value. isupper
返回的收益是一个逻辑值。 Testing that again with ==1
makes not much sense. ==1
进行测试没有多大意义。 input
, one in file scope, one in main
. input
,一个在文件范围内,一个在main
范围内。 Fairly simple: 相当简单:
#include <ctype.h>
/**
* Will return true if there's at least one alpha character in
* the input string *and* all alpha characters are uppercase.
*/
int allUpper( const char *str )
{
int foundAlpha = 0;
int upper = 1;
for ( const char *p = str; *p; p++ )
{
int alpha = isalpha( *p );
foundAlpha = foundAlpha || alpha;
if ( alpha )
upper = upper && isupper( *p );
}
return foundAlpha && upper;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.