凌晨小街 2019-12-05 23:59 采纳率: 0%
浏览 89

新手,写的思路清晰但很麻烦,发现看错了题就不知道怎样修改代码了,各位大佬能不能帮我看看,谢谢!!!(输入两个字符串样列 HELLO 与hello 输出一行样例 eEhHllLLoO 我错看为输出 eEhHlLlLoO)

#include
#include
using namespace std;

int main()
{
char a[105];
char b[105];
char s[105]={0},t[105]={0};
gets(a);
gets(b);
int f1=0,f2=0;
int len=strlen(b);
int i,j,k=0;
while(a[k]!='\0')
{
k++;
}
for(j=0;j<=len;j++)
{
a[k++]=b[j];
}
for(i=0;i {
if(a[i]>='a'&&a[i]<='z')
{
s[f1++]=a[i];
}
if(a[i]>='A'&&a[i]<='Z')
{
t[f2++]=a[i];
}
}

char temp1;
for(i=0;s[i]!='\0';i++)
{
for(j=i+1;s[j]!='\0';j++)
{
if(s[i]>=s[j])
{
temp1=s[i];
s[i]=s[j];
s[j]=temp1;
}
}
}
char temp2;
for(i=0;t[i]!='\0';i++)
{
for(j=i+1;t[j]!='\0';j++)
{
if(t[i]>=t[j])
{
temp2=t[i];
t[i]=t[j];
t[j]=temp2;
}
}
}
int j1=0,j2=0;
while(j1!=f1&&j2!=f2)
{
if(f1==f2)
{
cout<<s[j1];
cout<<t[j2];
j1++;
j2++;
}
else if(f1<f2)
{
if(j1!=f1)
{
cout<<s[j1];
cout<<t[j2];
j1++;
j2++;
}

else
{

cout<<t[j2];
j2++;

}
}
else
{

if(j2!=f2)
{
cout<<s[j1];
cout<<t[j2];
j1++;
j2++;
}

else
{

cout<<s[j1];
j1++;

}

}
}
cout<<endl;
return 0;
}

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 02:41
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    你的代码有一些问题。首先,你需要将输入的字符串转换成大写和小写字母,并且将它们分开存储在数组中。然后,你可以使用for循环来遍历这两个数组,找到匹配的部分并打印出来。

    以下是修改后的代码:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        string a, b;
        getline(cin, a); // read line from input
        getline(cin, b);
        
        transform(a.begin(), a.end(), a.begin(), ::tolower);
        transform(b.begin(), b.end(), b.begin(), ::toupper);
    
        int index_a = 0, index_b = 0;
        while (index_a < a.size() && index_b < b.size()) {
            if (a[index_a] == b[index_b]) {
                ++index_a;
                ++index_b;
            } else if (a[index_a] > b[index_b]) {
                ++index_b;
            } else {
                ++index_a;
            }
        }
    
        string result = "";
        while (index_a < a.size() || index_b < b.size()) {
            if (index_a < a.size()) {
                result += a[index_a++];
            }
            if (index_b < b.size()) {
                result += b[index_b++];
            }
        }
    
        cout << result << endl;
    
        return 0;
    }
    

    这个程序会读取两个字符串,将它们转换成大写和小写字母,然后查找匹配的部分并将它们合并成一个结果字符串。

    评论

报告相同问题?