1

# String operations (Id-2674)

Just for Practice 4 Additional [8-Jan-2018 to 31-Mar-2018]

CSE1002 Bonus Practice Sheet Winter 2018__Batch 3 [17-Mar-2018 to 7-Apr-2018]

Program ID- 2674

Design a class stringpos to perform the operations like retrieving the letter  in the i-th position (i-th symbol) of  a string, rotation of a string,  oddfirst  of a string, evenfirst of a  string, spin of two strings,  oddevenswap of two strings, evenoddswap of two strings.  Here the terms : rotation, oddfirst, evenfirst, are the operations performed on one string. The terms : spin, oddevenswap, evenoddswap are the operations performed on two strings.  With the string S and integer n<length of the string, rotation(S,n) is the string  S1 such that the the letter in the n-th position of S is the letter in the first position of S1, the letter in the (n-1)-th position of S is the second letter in S1, … the letter in the k-th  position is the (n-k+1)-th letter in S1 and so on. In the operation, rotation(S,n), ‘n’ is called the rotation index.  rotation(abcd,4) = dcba and rotation(abcd,2) = bacd.  oddfirst of a string S is got by placing all the letters occurring in the odd positions of S sequentially  and then placing all the letters occurring in the even positions of S, sequentially.  That is, oddfirst(abcd) is acbd. Similarly,  evenfirst of a string S is got by placing all the letters occurring in the even positions of S sequentially and then placing all the letters occurring in the odd positions of S, sequentially. That is, evenfirst(abcd) is bdac. Spin of two strings S1 and S2 is got by placing the first letter of S2 between the first and the second letter  of S1, placing the second letter  of S2 between the second and the third letter of S1 and so on.  That is, k-th letter of S2 will be placed between the k-th letter and the (k+1)-th letter  of S1.  If there are no k-th , (k+1)-th letters  in S1 to place the k-th letter of S2, then, the k-th symbol is just placed at the end of the string so far formed. For example, Spin(abcd, efgh)= aebfcgdh, Spin(ab, efgh) = aebfgh, Spin(abcd,ef)= aebfcd. Spin(S1, S2) need not be the same as that of Spin(S2, S1).

Oddevenswap(S1,S2) produces a string such that the  letters occurring in the odd positions  of S1 are  replaced by the letters occurring in the corresponding even positions of S2 and the letters occurring in the even position of S1 are left unchanged. In the above example, after a certain stage,  If there are no even-position letters in S2 to replace the odd-position letters in S1, those odd-position letters are left unreplaced.  For Example, oddevenswap(abcd, efgh)= fbhd, oddevenswap(ab,efgh)=fb, oddevenswap(abcd,fg)= gbcd.  Here also, oddevenswap(S1, S2) need not be same as that of oddevenswap(S2,S1).

Similarly, evenoddwap(S1,S2) produces a string such that the  letters occurring in the even  positions  of S1 are  replaced by the letters occurring in the corresponding odd  positions of S2 and the letters occurring in the odd  position of S1 are left unchanged.  For Example, evenoddswap(abcd, efgh)= aecg, evenoddswap(ab,efgh)=ae, evenoddswap(abcd,fg)= afcd  Here also, evenoddswap(S1, S2) need not be same as that of evenoddswap(S2,S1).

Overload shift left operator for rotation, prefix ++ for oddfirst, postfix ++ for evenfirst, ‘*’ for spin,’+’ operator for Oddevenswap, ‘-‘ operator for Evenoddswap, << and >> for I/O operations.

Input format:
First line contains the first string, S1

Next line contains the second string, S2

Rotation index n

Output Format
rotation(S1,n)

oddfirst(S2)

evenfirst(S1)

Spin(S1,S2)

Oddevenswap(S1,S2)

Evenoddswap(S1,S2)

Code

Please Comment Working if the code worked to you

If you have other working codes please comment the codes enclosing with <pre> and </pre> 🙂

C++ Code [By Chir Patel]

C++ Code

The Above Codes are for reference only. It doesn't mean everyone to directly copy/paste those codes.