What is the best practice for String input and output in CP?

I have doubts about the string input and output while solving the question.
For each test case, we take the string as input and print the same string with slide modification.
Modification: For every character in the string we have to replace that character with its next succession(alphabetically).
example:
qwerty -> rxfsuz
zub -> avc
Now the way I do this is

  • Begin the loop for every test case
  • Read the first string with cin>>_string_var;
  • Traverse through each character and printing that character with adding one for a successor(for z printing the a)
  • now the answer for the first Test case is successfully printed, cout<<"\n".
  • Moving to the next string and repeat…

Doubts:
Now is this the right approach to print the answer character by character? will this approach will get WA in any question?
Instead of printing the answer character by character I should create a temporary string and append the character to print the answer?
Or there is something else which I need to keep in mind?

You could create a new temp string, i would personally not recommend that but its your choice, You just need to keep in mind that you are not missing any edge case there might be. As for your solution I believe it should give AC, but you can never know without submitting it, as it may have some implementation error but the approach is right!

1 Like

if creating string is not recommended then what are the other ways?

He means that is you do not need to use that string later on, then instead of storing it, directly print the characters.

It increases Space Complexity and a lot of times it exceeds the given space limit given in complex problems and directly printing is the only other way i know.

1 Like