C语言标准函数库中包括 isalpha 函数,用于判断字母。作为练习,我们自己编写一个功能与之相同的函数。 函数原型为int IsAlpha(char x);说明:参数 x 是字符的 ASCII 码
时间: 2023-05-26 20:05:56 浏览: 129
实现:
```c
#include <stdio.h>
int IsAlpha(char x){
if((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')){
return 1;
}
else{
return 0;
}
}
int main(){
char a = 'a', b = 'B', c = '5';
printf("%d %d %d", IsAlpha(a), IsAlpha(b), IsAlpha(c));
return 0;
}
```
输出:
```
1 1 0
```
相关问题
c语言编写一个函数,统计一个输入字符串中字母、数字、空格和其他字符的个数
可以使用C语言中的字符函数库ctype.h中的函数来判断字符的类型。具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
void countChars(char *str, int *letters, int *digits, int *spaces, int *others) {
while (*str) {
if (isalpha(*str)) { // 判断字母
(*letters)++;
} else if (isdigit(*str)) { // 判断数字
(*digits)++;
} else if (isspace(*str)) { // 判断空格
(*spaces)++;
} else { // 其他字符
(*others)++;
}
str++; // 指针后移
}
}
int main() {
char str[100];
int letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入字符串:");
gets(str); // 读入字符串
countChars(str, &letters, &digits, &spaces, &others);
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
注意,上述代码使用了gets函数读入字符串,但是gets函数有安全性问题,可以使用fgets函数替代。同时,由于C语言中没有字符串类型,所以我们使用字符数组来表示字符串。
c语言编写一个函数,能够将字母电话号码转换成数字号码
好的,以下是一个将字母电话号码转换成数字号码的C语言函数:
```c
#include <ctype.h>
void letterToNum(char *phoneNum) {
while (*phoneNum) {
if (isalpha(*phoneNum)) {
switch (toupper(*phoneNum)) {
case 'A':
case 'B':
case 'C':
*phoneNum = '2';
break;
case 'D':
case 'E':
case 'F':
*phoneNum = '3';
break;
case 'G':
case 'H':
case 'I':
*phoneNum = '4';
break;
case 'J':
case 'K':
case 'L':
*phoneNum = '5';
break;
case 'M':
case 'N':
case 'O':
*phoneNum = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
*phoneNum = '7';
break;
case 'T':
case 'U':
case 'V':
*phoneNum = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
*phoneNum = '9';
break;
default:
break;
}
}
phoneNum++;
}
}
```
这个函数接受一个指向字符串的指针,遍历字符串并将字母转换成数字。我们使用了C标准库中的ctype.h头文件中的isalpha()函数来判断字符是否为字母,并使用了toupper()函数将字符转换成大写字母。然后使用一个switch语句将字母转换成对应的数字。最后,我们使用一个while循环来遍历整个字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)