C/C++[ w1785]字符串连接

W1785题目描述字符串连接

不借用任何字符串库函数实现接受两个字符串连接起来,并去除冗余项(重复的元素)

输入
每一行包括两个字符串,长度不超过100。

输出
可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。

样例输入
abbbcd defaj
样例输出
abcdefj

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    char str1[201] = {};
    char str2[101] = {};
    char ans[201] = {};
    while(cin>>str1 >>str2) {
        int l = strlen(str1);
        int j = 0;
        //连接连个字符串
        for (int i = 0; i < strlen(str2); i++)
            str1[l + i] = str2[i];
        //去掉连接后子字符串中的冗余项
        for (int i = 0; i < strlen(str1); i++) {
            if (str1[i] == ' ') continue;
            else {
                for (int j = 0; j < strlen(str1); j++) {
                    if (i == j || str1[j] == ' ') continue;
                    else if (str1[i] == str1[j]) str1[j] = ' ';
                }
            }
        }
        int k = 0;
        for (int i = 0; i < strlen(str1); i++) {
            if (str1[i] == ' ') continue;
            else ans[k++] = str1[i];
        }
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u014281392/article/details/80720248