C语言中输入一个字符串删除重复的字符若输入的是小写则输出大写
以下是一个示例程序,可以实现输入一个字符串,删除重复的字符,并将小写字符转换为大写输出。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void removeDuplicates(char* str) {
int len = strlen(str);
int index = 0;
for (int i = 0; i < len; i++) {
int j;
for (j = 0; j < i; j++) {
if (str[i] == str[j]) {
break;
}
}
if (j == i) {
str[index++] = islower(str[i]) ? toupper(str[i]) : str[i];
}
}
str[index] = '\0';
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
removeDuplicates(str);
printf("删除重复字符后的字符串为:%s\n", str);
return 0;
}
运行示例:
请输入一个字符串:aabBcCdD
删除重复字符后的字符串为:ABCD
在示例程序中,removeDuplicates函数用于删除重复的字符。它通过两层循环遍历字符串中的每个字符,判断是否重复出现。如果没有重复出现,则将字符添加到结果字符串中,并将小写字符转换为大写。最后将结果字符串的末尾置为字符串结束符。
在main函数中,首先通过scanf函数获取用户输入的字符串,并将其传递给removeDuplicates函数进行处理。最后打印处理后的字符串
原文地址: https://www.cveoy.top/t/topic/idcc 著作权归作者所有。请勿转载和采集!