C语言:关于字符串的编程题

题目如下:
You’ve gathered some e-mail addresses from a variety of sources, and you want to send out a mass mailing to all of the addresses. However, you don’t want to send out duplicate messages. You need to write a program that reads all e-mail addresses and discards any that already have been input.

Input
The first line is a positive integer for the number of e-mail addresses which is smaller than 50. Then each of the e-mail addresses is input in one line.

Output
Output the new mailing list in lexicographic order. Each e-mail address in one line.

Note: ignore letter case when comparing two e-mail addresses, but the output is case sensitive.

Sample Input
10
toocle01@netsun.com
chuangling@chuangling.net
zjykrc@163.com
hahdjx@163.com
5663@sohu.com
toocle01@netsun.com
chenql_008@163.com
tsmoql@alibaba.com.cn
LYC@hzlasiji.com
zjykrc@163.com
Sample Output
5663@sohu.com
chenql_008@163.com
chuangling@chuangling.net
hahdjx@163.com
LYC@hzlasiji.com
toocle01@netsun.com
tsmoql@alibaba.com.cn
zjykrc@163.com

题目的意思是输入一堆电子邮箱的地址,让我们设计一个程序,能够剔除重复的邮箱地址并将邮箱地址按照字母表顺序排列。在比较字符串的时候,字母的大小写可以忽略,但是在输出的时候,字母的大小写要纳入考虑的范围。即根据ASCII。

# include <stdio.h>
# include <string.h>
char* strlwr(char copy[100]) {
    for (int i = 0; i < strlen(copy); i++) {
        if (copy[i] <= 'Z' && copy[i] >= 'A')
            copy[i] += 32;
    }
    return copy;
}
int main(void) {
    int T;
    scanf("%d", &T);
    char address[50][100];
    int i = 0, len = 0;
    while (i < T) {
        i++;
        scanf("%s", address[len]);
        for (int j = 0; j < len; j++) {
            if (strcmp(address[len], address[j]) == 0) {
                len--;
                break;
            }
        }
        len++;
    }
    for (int j = 0; j < len-1; j++)
        for (int k = 0; k < len-1-j; k++) {
            char copy2[100], copy3[100];
            snprintf(copy2, sizeof(copy2), "%s", address[k]);
            snprintf(copy3, sizeof(copy3), "%s", address[k+1]);
            if (strcmp(strlwr(copy2), strlwr(copy3)) > 0) {
                char copy[100];
                snprintf(copy, sizeof(copy), "%s", address[k]);
                snprintf(address[k], sizeof(address[k]), "%s", address[k+1]);
                snprintf(address[k+1], sizeof(address[k+1]), "%s", copy);
            }
        }
    for (int j = 0; j < len; j++) {
        printf("%s\n", address[j]);
    }
    return 0;
}

以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨!


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值