Question

In: Computer Science

For these of string functions, write the code for it in C++ or Python (without using...

For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type.

1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str

Solutions

Expert Solution

PYTHON CODE:

#
#   Python program to find positions of a substring in the given string
#
def searchA(in_str, sub):
    # Empty list containing positions of substrings (if exists)
    pos = []
    # Search in_str
    for i in range(len(in_str)):
        matched = True
        # If in_str's character matches with first character of substring
        if in_str[i] == sub[0]:
            # Check if the rest of the characters matches or not
            for j in range(i + 1, len(sub) + i):
                if j >= len(in_str) or in_str[j] != sub[j - i]:
                    matched = False
                    break
            # If all characters matched then append the starting index of sub in in_str
            if matched:
                pos.append(i)
    # If substring was not found then append -1 to pos list
    if len(pos) == 0:
        pos.append(-1)
    return pos
in_str = "aaabbbabbababaab"
sub1 = "ab"
sub2 = "abbc"
print(searchA(in_str, sub1))
print(searchA(in_str, sub2))

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT


Related Solutions

I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x...
Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x + 4.
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT