Say you need to write a function such that you have string a and string b. The function argument is a concatenation of string a and string b. The function prints string a and string b separately. I'll explain.

For example one can think of a naive solution such that 'x' will be a separator character and so if we want to print "hello world" for example we create a function that prints everything as string a until it encounters an 'x' char and than it knows that string b is starting. So our argument will be something like:

void separateString("helloxworld");

But this eliminates the option of using 'x' as a char in our String.

I am sure that there is a simple solution to this that I am not getting.

Not looking for code, just your idea. The code will be easy to implement.


  • 1
    $\begingroup$ Pass the length of the first string? $\endgroup$ – Evil Sep 25 '17 at 8:43
  • 2
    $\begingroup$ en.wikipedia.org/wiki/Prefix_code ​ ​ $\endgroup$ – user12859 Sep 26 '17 at 0:37

Just an extended note for the "quoting" techniques:

You can also pick one character of the alphabet (e.g. $a$) and interpret it as a space and use $aa$ for $a$:

 hello world  -> helloaworld
 amazing spacing -> aamaazingaspaacing

However it is easy to see that this approach can lead to ambiguities:

 a are doubled -> aaaaare doubled 
                 --OK_DECODING   -> a are doubled
                 --WRONG_DECODING-> aa re doubled

These ambiguities can be avoided/ignored if we assume that the "special" character $a$ appears only in a well defined format and context.

Indeed this technique is still used for representing quotes or double quotes in well-known formats: e.g it is used for strings in the CSV format: every string is double quoted and the double quotes in a string are "doubled".

 COLUMN 1 | COLUMN 2            | COLUMN 3
 42        | My name is "Nobody" | He said "To be or not to be" 
 43        | "Quote me"          | Charater " embedded

Are represented in CVS files as:

 42,"My name is ""Nobody""","He said ""To be or not to be"""
 43,"""Quote me""","Character "" embedded"

It is also used for single quotes in the SQL language:

 SELECT 'This is a quoted string: ''Hello world''!!!'

But more modern languages/formats usually prefer the escape technique (see Yuval's answer)


The simplest approach is to use escape characters. Suppose that you want x to be your separator. To escape it, you use \x (that would mean x). To escape \, you use \\ (that would mean \).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.