Question

In: Computer Science

5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter...

5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter letter (as a string) and returns a list of indexes at which the letter occurs in the word.

>>> indexes('mississippi', 's')

[2, 3, 5, 6]

>>> indexes('mississippi', 'i')

[1, 4, 7, 10]

>>> indexes('mississippi', 'a')

[]

Solutions

Expert Solution

def indexes(n,n1):
    l=[]
    j=0
    for i in range(len(n)):
        if(n[i]==n1):
            l.append(i)
    return l



n=input("Enter a string : ")
n1=input("Enter a character to check : ")
if(len(n1)!=1):
    print("You enter only one character ")
    n1=input("Enter a character to check : ")
print(indexes(n,n1))

Here is the python program that will asks the user to enter the string and then asks the user to enter a character and we call the function indexes() and in that function we are will iterate through the loop and then we compare each character of the string to the character and then append them to the list and finally return the list and print it.

SCREENSHOT OF THE OUTPUT AND THE CODE:

You can refer the photo for intendation in the code as python uses intendation.

I am providing you the code in ajva below but for exact output you better use python

import java.util.*;
class Main
{
        public int[] insert(String s,char c)
        {
                int [] a=new int[s.length()];
                int j=0;
                
                for(int i=0;i<s.length();i++)
                {
                        if(s.charAt(i)==c)
                        {
                                a[j]=i;
                                j++;
                        }
                }
                return a;
        }
        
        public static void main(String args[])
        {
                Scanner sc=new Scanner(System.in);
                Main m=new Main();
                System.out.print("Enter the string : ");
                String s=sc.nextLine();
                System.out.print("Enter a character to search : ");
                char c = sc.next().charAt(0); 
                int a[]=m.insert(s,c);
                for(int i=0;i<s.length();i++)
                {
                        if(a[i]==0)
                                break;
                        System.out.print(a[i]+" ");
                }
                }
        }

Here the screenshot of th code is not provided as there is no need of intendation in java.

SCREENSHOT OF THE OUTPUT:


Related Solutions

4.31 Implement function duplicate() that takes as input the name (a string) of a file in...
4.31 Implement function duplicate() that takes as input the name (a string) of a file in the current directory and returns True if the file contains duplicate words and False otherwise. duplicate('Duplicates.txt') True duplicate('noDuplicates.txt') False Please solve using Python Language and without using str.maketrans please. Just simple programming, Thank youuuuu!!!!!
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
Implement function noVowel() that takes a string s as input and returns True if no char-...
Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10])...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function bracket_by_len that takes a word as an input argument and returns the word...
Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space...
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input...
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input string will be a sentance ex: "The cat is on the chair and the bear is on the chair") -- returns a table (word varchar(max), count int) (Output is a table that shows how many times each word appeared in the sentance) Where each space-delimited “word” in the string appears in the table along with the number of times it appeared in the input...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT