1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
almond37 [142]
3 years ago
10

Building a String Library

Computers and Technology
1 answer:
maw [93]3 years ago
6 0
Code:

def myAppend( str, ch ):
# Return a new string that is like str but with
# character ch added at the end
return str + ch

def myCount( str, ch ):
# Return the number of times character ch appears
# in str.

# initiaalizing count with 0
count = 0

# iterating over every characters present in str
for character in str:
# incrementing count by 1 if character == ch
if character == ch:
count += 1

# returning count
return count


def myExtend( str1, str2 ):
# Return a new string that contains the elements of
# str1 followed by the elements of str2, in the same
# order they appear in str2.

# concatenating both strings and returning its result
return str1 + str2

def myMin( str ):
# Return the character in str with the lowest ASCII code.

# If str is empty, print "Empty string: no min value"
# and return None.
if str == "":
print("Empty string: no min value")
return None

# storing first character from str in char
char = str[0]

# iterating over every characters present in str
for character in str:
# if current character is lower than char then
# assigning char with current character
if character < char:
char = character
# returning char
return char


def myInsert( str, i, ch ):
# Return a new string like str except that ch has been
# inserted at the ith position. I.e., the string is now
# one character longer than before.

# Print "Invalid index" if
# i is greater than the length of str and return None.

if i > len(str):
print("Invalid index")
return None

# str[:i] gives substring starting from 0 and upto ith position
# str[i:] gives substring starting from i and till last position
# returning the concatenated result of all three
return str[:i]+ch+str[i:]

def myPop( str, i ):
# Return two results:
# 1. a new string that is like str but with the ith
# element removed;
# 2. the value that was removed.
# Print "Invalid index" if i is greater than or
# equal to len(str), and return str unchanged and None
if i >= len(str):
print("Invalid index")
return str, None

# finding new string without ith character
new_str = str[:i] + str[i+1:]

# returning new_str and popped character
return new_str, str[i]

def myFind( str, ch ):
# Return the index of the first (leftmost) occurrence of
# ch in str, if any. Return -1 if ch does not occur in str.

# finding length of the string
length = len(str)

# iterating over every characters present in str
for i in range(length):
# returning position i at which character was found
if str[i]==ch:
return i
# returning -1 otherwise
return -1


def myRFind( str, ch ):
# Return the index of the last (rightmost) occurrence of
# ch in str, if any. Return -1 if ch does not occur in str.

# finding length of the string
length = len(str)

# iterating over every characters present in str from right side
for i in range(length-1, 0, -1):
# returning position i at which character was found
if str[i]==ch:
return i
# returning -1 otherwise
return -1

def myRemove( str, ch ):
# Return a new string with the first occurrence of ch
# removed. If there is none, return str.

# returning str if ch is not present in str
if ch not in str:
return str

# finding position of first occurence of ch in str
pos = 0

for char in str:
# stopping loop if both character matches
if char == ch:
break
# incrementing pos by 1
pos += 1

# returning strig excluding first occurence of ch
return str[:pos] + str[pos+1:]

def myRemoveAll( str, ch ):
# Return a new string with all occurrences of ch.
# removed. If there are none, return str.

# creating an empty string
string = ""

# iterating over each and every character of str
for char in str:
# if char is not matching with ch then adding it to string
if char!=ch:
string += char
# returning string
return string

def myReverse( str ):
# Return a new string like str but with the characters
# in the reverse order.

return str[::-1]
You might be interested in
Why does my playstation keep disconnecting from wifi?.
REY [17]

Answer:

because there might be a internet

problem

Explanation:

5 0
2 years ago
When creating an electronic slide presentation, Lee should avoid
Amanda [17]

I believe the answer is <u>Using sound effects between slides.</u>

Using sound effects between slides can cause for a distraction, and if you are in college, your professor may not score your presentation as well as if it were made without sound effects. Hope this helps!

5 0
3 years ago
Write a C program to show sum of 10 elements of array and show the average.
ipn [44]

Answer:

// C program to find sum and average of 10 elements.

#include <stdio.h>

// main function

int main(void) {

   // array

double arr[10];

// variables

double average,sum=0;

int i;

   // ask to enter 10 elements of the array

printf("Enter 10 elements of the array:");

for(i=0;i<10;i++)

{

    // read elements of the array

    scanf("%lf", &arr[i]);

    // calculate the sum of elements

    sum=sum+arr[i];

}

// fint the average

average=sum/10;

// print sum

printf("Sum of 10 elements of the array is:%f ",sum);

// print average

printf("\nAverage of 10 elements of the array is:%f ",average);

return 0;

}

Explanation:

Create an array of size 10.Then read 10 values from user and store them into  array.Find the sum of 10 elements of the array and assign it to variable "sum". Find the average by dividing sum with 10.Print the sum and average.

Output:

Enter 10 elements of the array:3 2 4 5 1 4 8 9 12 10                                                                      

Sum of 10 elements of the array is:58.000000

Average of 10 elements of the array is:5.800000

8 0
3 years ago
What are the two main factors in the mantle that contribute to the rock cycle?
meriva
Pressure and heat I believe is the answer not sure
8 0
3 years ago
Read 2 more answers
What is true of softboxes?
gizmo_the_mogwai [7]
Softboxes have an umbrella on the front. The answer to your question is A. I hope that this is the answer that you were looking for and it has helped you.
4 0
4 years ago
Read 2 more answers
Other questions:
  • Ideation includes all of the following EXCEPT
    5·1 answer
  • Collins and quillian explained the results of priming experiments by introducing the concept of _____ into their network model.
    12·1 answer
  • Which option allows you to customize the order of your data ?
    8·2 answers
  • What method does a GSM network use to separate data on a channel?
    15·1 answer
  • 5. Write a function that takes two lists of integers and returns a list containing tuples with corresponding elements from both
    8·1 answer
  • A #1 Phillips screwdriver has a shaft diameter of 
    13·1 answer
  • To apply the StartSafe philosophy to preventing workplace violence, what are the three steps to follow? A. Plan for your job req
    12·2 answers
  • When was the very first computer made??
    5·1 answer
  • What is the best file manager for windows 10?
    5·2 answers
  • 5. What are some situations where you might find it useful to use the “!” symbol in a program?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!