# String Exercises ## Exercise 1.1 (Search) ```python def search(x: str, s:str) -> int: """ Function that counts the number of occurences of a given character in a string @params x (str): the keyword @params s (str): the string @return: (int): the number of occurences """ i = 0 pos = -1 while i < len(s) and pos == -1: if s[i] == c: pos = i i += 1 return pos def search2(s: str, x: str) -> int: """ Function that counts the number of occurences of a given character in a string @params x (str): the string @return: (int): the occurence where is appear """ i = 1 n = len(s) for i in range(n): if s[i] == x: return i return -1 ``` ## Exercise 1.2 (Palindrome) ```python def palindrome(sentence: str) -> bool: """ Function that check if a number is a palindrome @params sentence(str): the sentence to check @return (bool): Either a palindrome or not """ newSentence = "" oldSentence = "" for element in sentence: if element != " ": newSentence = element + newSentence for element in sentence: if element != " ": oldSentence += element if newSentence == oldSentence: return True else: return False def ispalind(s): start = 0 end = len(s)-1 while start= end ``` ## Exercise 2.1 (conversions) 1) ```python def dec_to_bin(n, p): s = "" while n > 0: s = str(n%2) +s n = n//2 for i in range(p-len(s)): s = "0" + s return s def integer_to_twoscomp(n, p): if n < 0: n = power(2, p) + n return dec_to_bin(n, p) def bin_to_dec(s): n = 0 for b in s: n = n * 2 + int(b) return b ``` 2) ```python def power(x, p): res = 1 while p > 0: res *= x p -= 1 return res def int_to_twoscomp(n, p): if n < 0: n = power(2,p) + n return dec_to_bin(n,p) ``` ## Exercise 2.2 (Frequency) ```python def frequency(s: string): count = 0 countInter = 0 elementMax = "" index = 0 foreach elem in s: for i in range(index, lenght(s)): if s[i] == elem: countInter += 1 if countInter > count: count = countInter elementMax = elem index += 1 return (cout, elementMax) ```