c语言将数组中的字母准换成大小写char arr[]={ 'a ' , 'S' , 'A', 's ' , 'w ','W'};
时间: 2023-10-23 11:25:29 浏览: 76
下面是将数组中的字母转换成大写字母的代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char arr[] = { 'a', 'S', 'A', 's', 'w', 'W' };
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
if (islower(arr[i])) {
arr[i] = toupper(arr[i]);
}
}
for (int i = 0; i < n; i++) {
printf("%c ", arr[i]);
}
return 0;
}
```
输出结果:
```
A S A S W W
```
如果要将数组中的字母转换成小写字母,只需将 `islower()` 函数改为 `isupper()` 函数,将 `toupper()` 函数改为 `tolower()` 函数即可。
相关问题
用c语言写出一个能统计数组中字母个数的程序,并且自定义函数,调用到主函数里
下面是一个可以统计数组中字母个数的 C 语言程序,其中包含自定义函数 `countLetters`,并在主函数中调用:
```c
#include <stdio.h>
#include <ctype.h>
void countLetters(char arr[], int size, int counts[]) {
for (int i = 0; i < size; i++) {
if (isalpha(arr[i])) {
counts[toupper(arr[i]) - 'A']++;
}
}
}
int main() {
char arr[] = {'a', 'b', 'c', 'D', 'E', 'F', '1', '2', '3'};
int counts[26] = {0};
int size = sizeof(arr) / sizeof(arr[0]);
countLetters(arr, size, counts);
for (int i = 0; i < 26; i++) {
printf("%c: %d\n", 'A' + i, counts[i]);
}
return 0;
}
```
在上面的程序中,`countLetters` 函数接受三个参数:一个字符数组 `arr`,数组大小 `size`,以及一个用于记录字母个数的整数数组 `counts`。函数使用 `isalpha` 判断字符是否为字母,如果是字母,则将其转换为大写并在 `counts` 数组相应位置上加 1。
在主函数中,我们定义了一个字符数组 `arr`,包含了字母和非字母字符。然后定义一个长度为 26 的整数数组 `counts`,用于记录每个字母出现的次数。我们使用 `sizeof` 运算符计算出 `arr` 数组的大小,并将大小和 `counts` 数组传递给 `countLetters` 函数。最后,在循环中打印出每个字母出现的次数。
用C语言编写一个判断字符串子串与一个字符串数组中的元素有无相同的函数,忽略大小写
以下是一个用C语言编写的判断字符串子串与一个字符串数组中的元素有无相同的函数,忽略大小写的示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int is_substring(char *str, char **arr, int len) {
int i, j;
for (i = 0; i < len; i++) {
if (strstr(arr[i], str) != NULL) {
return 1;
}
for (j = 0; arr[i][j] != '\0'; j++) {
arr[i][j] = tolower(arr[i][j]);
}
if (strstr(arr[i], str) != NULL) {
return 1;
}
}
return 0;
}
int main() {
char *arr[] = {"Hello", "world", "CSDN", "AI"};
char *str1 = "csdn";
char *str2 = "openai";
printf("%d\n", is_substring(str1, arr, 4));
printf("%d\n", is_substring(str2, arr, 4));
return 0;
}
```
该函数接受一个字符串和一个字符串数组,以及数组的长度作为参数,返回一个整数值。如果字符串是数组中任意一个元素的子串(忽略大小写),则返回1,否则返回0。在函数中,我们首先使用标准库函数strstr()来判断字符串是否是数组中任意一个元素的子串,如果是,则直接返回1。如果不是,则将数组中每个元素的字母全部转换为小写,再次使用strstr()函数进行判断。最后,如果都不是,则返回0。
注意,我们在函数中使用了标准库函数tolower()来将字母转换为小写。
相关推荐
![](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)