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
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Sample Output
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

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

# include 
# include 
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;
}

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


你可能感兴趣的:(C语言)