C++20 sections · 1024 units
Open in Course

Petya and Strings - Implementation

Complete solution


plaintext
function compareStrings(s1, s2)
n := length of s1
for i from 0 to n - 1
c1 := tolower(s1[i]) c2 := tolower(s2[i])
if c1 < c2 then
 return -1
 if c1 > c2 then
 return 1
 return 0

You convert characters at each position, compare them, and return immediately when you find a difference. If no difference exists after checking all positions, the strings are equal. Converting to lowercase normalizes the comparison. Then standard string comparison works character by character. The first difference determines the result. If strings are equal after case conversion, output 0.