FreeChat Online

It is 1985, and you are a humble Russian potato farmer. Sadly, your glorious homeland is going through a time of crisis and it needs your help! The KGB, with the "assistance" of your wife and children, has convinced you to build their up-and-coming government FreeChat Online service for high ranking soviet officials. Luckily, the Russian government has provided you with several items seized through routine surveillance of the US's technological innovations—a cutting edge Macintosh XL, and the prototype source code for LucasArts's Habitat, an advanced communication service with cutting edge graphics. Your task is simple: create a few modifications for the Russian government…

Due to Communism's tendency to coalesce toward collectivistic cultures, the Russian government wants this chat to embody their perfect world—one without silly notions of self or individualism. Alas, Macintosh compatibility leads you astray. Due to problems in the prototype, every other sentence in the chat buffer is corrupted, except for the ending period. Lucky for you, the Russian people make classification easy for you.

CHALLENGE: For a String s in the input, there are 1 or more sentences, separated by ". "(period, TWO spaces) The 2nd, 4th, 6th, and so on sentences are corrupted. The last sentence may have nothing, ".", ". ", or ". " as its end. Here's some examples of an input:

"L'etat c'est moi (I am the state) - King Louis XIV"

|----------------one sentence, ended by nothing--------------|

"Hello World. GojdGojog. How are you. "

|--three sentences, one corrupted, ended by ". "--|

"Hi. gfhyG. My favorite food is Baseball. jijhojh. Foobar. "

|---------------Five sentences, two corrupted, ended by ". "-----------|

You must process this string, replacing any capitalized word besides the first word of a sentence, words in parentheses (no nested, but there can be multiple sets in a sentence), or corrupted sentences with the string "The State".


"L'etat c'est moi (I am the state) - King Louis XIV"

--->"L'etat c'est moi (I am the state) - The State The State The State"

"Hello World. GojdGojog. How are you. "

--->"Hello The State. GojdGojog. How are you. "

"Hi. gfhyG. My favorite food is Baseball. jijhojh. Foobar. "

--->"Hi. gfhyG. My favorite food is The State. jijhojh. Foobar. "


  1. Standard Loopholes are prohibited

  2. ASCII Text Input

  3. If the input doesn't match the specifications set above, do whatever you please with the output!

  4. This is code-golf, so the shortest(valid)answer (in each language) wins!

  5. Bonus points for a one-liner 😎 (Not really tho, just cool points)

  • 2
    \$\begingroup\$ I had thought this post is a spam when I read its title in questions list. \$\endgroup\$
    – tsh
    Nov 14, 2018 at 5:05
  • 2
    \$\begingroup\$ Why Hi. gfhyG. My favorite food is Baseball. jijhojh. Foobar. is 4 sentences? Should it be 3? Hi., gfhyG., My favorite food is Baseball. jijhojh. Foobar. \$\endgroup\$
    – tsh
    Nov 14, 2018 at 5:12
  • 2
    \$\begingroup\$ @Michael Why? Where are the another 2? Should dot followed by one space considered as stop of a sentence? \$\endgroup\$
    – tsh
    Nov 14, 2018 at 5:15
  • 2
    \$\begingroup\$ For the first example, is it unintended that the "C" is capitalized in the input? \$\endgroup\$ Nov 14, 2018 at 6:15
  • 1
    \$\begingroup\$ You state "The 2nd, 4th, 6th, and so on sentences are corrupted.", but your first example is only a single sentence, so why are King Louis XIV replaced with 3x The State? Also, as mentioned by @KamilDrakari above, why isn't the C (or C'est) not replaced with The State while the King Louis XIV are? \$\endgroup\$ Nov 14, 2018 at 9:08

3 Answers 3


Retina 0.8.2, 77 bytes

(?<=^(([^.]*)\. +([^.]*)\. +)*[^ .][^.]*)(?<!\([^)]*)[A-Z][A-Za-z]*
The State

Try it online! Link includes test cases. Explanation:

(?<=^(([^.]*)\. +([^.]*)\. +)*[^ .][^.]*)

Only match in the middle of alternate sentences.


Don't match inside parentheses.


Match words beginning with uppercase letters.

The State

Replace them as desired.


Java 10, 164 bytes

s->{var S=s.split(".  ",-1);for(int l=S.length,i=0;i<l;)System.out.print((i%2<1?S[i].replaceAll("(?!^)[A-Z]\\w*(?<!\\([^)]*)","The State"):S[i])+(++i<l?".  ":""));}

Try it online.


s->{                       // Method with String as both parameter and return-type
  var S=s.split(".  ",-1); //  Split the input by ".  " to get the list of sentences
                           //  (the -1 is in case the input ends in a trailing ".  ")
  for(int l=S.length,      //  Amount of sentences
      i=0;i<l;)            //  Loop `i` in the range [0, amount_of_sentences):
    System.out.print(      //   Print (without newline):
     (i%2<1?               //    If `i` is even:
       S[i]                //     Print the sentence,
        .replaceAll(       //      After we've replaced all
          "(?!^)           //       (except the very first)
           [A-Z]\\w*       //       capitalized words,
           (?<!\\([^)]*)"  //       that are not within parenthesis
          "The State")     //      With "The State"
      :                    //    Else:
       S[i])               //     Print the sentence as is
            +(++i<l?       //    And if it's not the last iteration:
               ".  "       //     Append ".  "
              :            //    Else:
               ""));}      //     Append nothing more

Perl 6, 91 bytes

{S:g/\s<:Lu><-[.\s]>+<?{!grep {$/.prematch.comb($_)%2},'.  ',/\(|\)/,/\.\s$/}>/ The State/}

Try it online!

Regex based solution.


 S:g/                  # Do a global substitution
     \s<:Lu><-[.\s]>+  # On all words starting with capital letters
               <?{                                        }>  # Where:
                  !grep {                       } # None of:
                                                 ,'.  '   # Sentence terminators
                                                 ,/\(|\)/ # Parenthesises
                                                 ,/\.\s$/ # Is first word of sentence?
                                    .comb($_)%2   # Have an odd amount of matches
                         $/.prematch              # From the string before
                                                           / The State/  # And replace

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