490. Coding

 

Sequence of bits is encoded as follows. If the previous bit differs from the current encoded bit, write 1 to resulting sequence. If the values of bits are different, then write 0. For the first bit of the sequence the previous bit has value 0.

Write a program that performs encoding.

 

Input. One string with no more than 100 characters, consisting only of 0 and 1, representing a coded sequence of bits.

 

Output. Print the resulting code.

 

Sample input

Sample output

10010111

11011100

 

 

SOLUTION

string

 

Algorithm analysis

In this problem you must simulate the coding process.

 

Algorithm realization

Array s contains the input string, array t contains the resulting string.

 

char s[110], t[110];

 

Read the input line. Initially, the previous character is 0.

 

gets(s);

prev = '0';

 

Encode the characters according to the rule given in the problem statement.

 

for (i = 0; i < strlen(s); i++)

{

  if (s[i] == prev) t[i] = '0'; else  t[i] = '1';

  prev = s[i];

}

 

At the end of line t put zero byte. Print the resulting code.

 

t[i] = 0;

puts(t);

 

Algorithm realization – C++

String s contains the input string, string res contains the resulting string.

 

string s, res;

 

Read the input line. Initially, the previous character is 0.

 

cin >> s;

p = '0';

 

Encode the characters according to the rule given in the problem statement.

 

for (i = 0; i < s.size(); i++)

{

  if (s[i] == p) res = res + '0';

  else res = res + '1';

  p = s[i];

}

 

Print the resulting code.

 

cout << res << endl;