Def testword string result True strlen len string for i in

Def test_word (string): result = True str_len = len (string) for i in range (, int (str_len/2)): if string[i] ! = string[str_len-i-1]: result = False break return result print(test_word(\'hello\')) print(test_word(\'madam\')) Output

Solution

the above logic is to test whether a string is palidrome or not

in the print statement ie:printf(test_word(\'hello\')); it will give false beacuse h!=o ,it will break the loop in the first iteration only

in the second print statement ie:printf(test_word(\'madam\')); ,it will give true beacuse in the first iteration m==m so condition satisfied next do the second iteration a==a ,it is satisfied and finally at last iteration d==d so at end it will give true.

 Def test_word (string): result = True str_len = len (string) for i in range (, int (str_len/2)): if string[i] ! = string[str_len-i-1]: result = False break ret

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site