【问题标题】:comparing against both lowercase and uppercase.比较小写和大写。
【发布时间】:2016-11-24 23:15:07
【问题描述】:

如果适用,感恩节快乐。我正在尝试创建一个将来可以使用的功能。该函数应该能够接受一个字符数组并对其进行编码。 (代码部分我还没写完)

int codetut(char *codeit){
int x,y = 0;
char tmparry[strlen(codeit)];
while(codeit[x] != '\0'){
    if((codeit[x] >= 'a' && codeit[x] <= 'z') || (codeit[x] >= 'A' && codeit[x] <= 'Z')){ //Set to look for capitals and commons
        if((codeit[x] == 'a' || codeit[x] == 'e' || codeit[x] == 'i' || codeit[x] == 'o' || codeit[x] == 'u') && (codeit[x] == 'A' || codeit[x] == 'E' || codeit[x] == 'I' || codeit[x] == 'O' || codeit[x] == 'U')){ // set to look for vowels
        tmparry[y] = codeit[x];

        x++,y++;
        }else{

        x++,y;
        }
    }else{
    tmparry[y] = codeit[x];
    x++,y++;
    }
}
printf("%s",tmparry);
return 0;
}

我在看这里how to validate both lowercase and uppercase letters。不幸的是,我没有找到我要找的东西。

问题:

  1. 有没有更好的方法将指针与大写和小写版本进行比较?

只有一个请求,如果您知道对我有帮助的帖子,请指出。

【问题讨论】:

  • isalpha。 isvowel 在标准 C 中不存在。int x --> int x = 0, char tmparry[strlen(codeit)]; --> char tmparry[strlen(codeit)+1]; 然后空终止。
  • @BLUEPIXY 是的,这是有道理的。谢谢谢谢 :) 我没有看到这会在未来造成什么问题。
  • 顺便说一句,你有一个明显的逻辑错误,一个字母不能既是小写元音又是大写元音。
  • @djchlin 好吧,我的目标是在 a -> z 之间,对于每个不是元音的字母都在其末尾添加一个代码,然后将其附加到新数组中。跨度>
  • @DamaniAPhilip ,djechlin 的意思是第二个 if 语句有错误,一个 char 不能同时为大写和小写,一个或另一个。

标签: c


【解决方案1】:

先将字符转换为低,然后进行比较。

tolower 就是这个方法。参见例如c - convert a mixed-case string to all lower case

【讨论】:

  • 谢谢,我现在就去看看。 :)
猜你喜欢
  • 2010-11-07
  • 2020-05-23
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
相关资源
最近更新 更多