Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Given two strings S1 and S2 arrange the charecters of S1in same alphabetical order as the charecter of s2.

0 votes
asked Aug 13, 2019 by arjun

1 Answer

0 votes
answered May 3, 2025 by Nirkos Scarpt (140 points)

You can create another string, let's call it S3,  and then search S1 for every charaster of S2 and if the character is found, add it to S3.

C++ example:

#include <iostream>
#include <cstring>

int main()
{
    std::string s1, s2, s3;
    std::cin >> s1 >> s2;
    
    for (int i = 0; i < s2.length(); ++i)
    {
        for (int j = 0; j < s1.length(); ++j)
        {
            if (s1[j] == s2[i])
            {
                s3 = s3 + s1[j];
            }
        }
    }
    
    std::cout << s3;

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...