实现函数int dup(char *str)函数名: dup 功能:将数组str[]中的小写字母变成大写字母,并把其中的数字去掉如:Hello2010Word2011 !HELLOWORD!) 参数: str[]字符串数组 返回值,返回去掉了多少个数字
时间: 2024-03-08 20:51:02 浏览: 22
以下是实现函数int dup(char *str)的代码:
```c
#include <stdio.h>
#include <ctype.h>
int dup(char *str) {
int count = 0;
int i, j;
for (i = 0, j = 0; str[i] != '\0'; i++) {
if (isdigit(str[i])) {
count++;
} else if (islower(str[i])) {
str[j++] = toupper(str[i]);
} else if (isupper(str[i])) {
str[j++] = str[i];
}
}
str[j] = '\0';
return count;
}
int main() {
char str[] = "Hello2010Word2011";
int count = dup(str);
printf("%s\n", str);
printf("Removed %d digits.\n", count);
return 0;
}
```
在dup函数中,我们遍历输入的字符串数组,对于每个字符,如果它是小写字母,我们将其转换成大写字母并添加到输出的新字符串数组中;如果它是大写字母,我们直接添加到新字符串数组中;如果它是数字,我们将计数器count加1。最后,我们将新字符串数组的末尾设置为null字符,以便printf函数能正确输出新字符串。最后,我们在主函数中调用dup函数,并输出处理后的新字符串和删除的数字数。