Question

having trouble with the following swift 5 problem. It is just printing the number 4 twice...

having trouble with the following swift 5 problem. It is just printing the number 4 twice and it seems to be ignoring camelCasePhraseTwo, please help if you can:

/* Problem 4

Write a function that takes in a camel-case string and returns an integer representing the number of words in the string

Notes:

- Assume all input will contain only letters, and the string will be a valid camel-cased phrase

- Do not loop through the array manually.

*/

func camelCaseWordCount(for camelCasePhrase: String) -> Int{

var camelCasePhraseOneCount: Int = 1

var camelCasePhraseTwoCount: Int = 1

let lowercase: Set<Character> = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

camelCasePhraseOne.removeAll(where: { lowercase.contains($0) })

camelCasePhraseTwo.removeAll(where: { lowercase.contains($0) })

camelCasePhraseOneCount = camelCasePhraseOne.count

camelCasePhraseTwoCount = camelCasePhraseTwo.count

return camelCasePhraseOneCount + 1

return camelCasePhraseTwoCount

}

//// Sample Input

var camelCasePhraseOne = "camelCasePhraseOne"

var camelCasePhraseTwo = "aMuchLongerCamelCasePhraseThatIsWayWayWayMoreFunToTest"

//

//// Sample Output

print(camelCaseWordCount(for: camelCasePhraseOne)) // 4

print(camelCaseWordCount(for: camelCasePhraseTwo)) // 15

0 0
Add a comment Improve this question Transcribed image text
Answer #1

func camelCaseWordCount(for camelCasePhrase: String) -> Int{

var camelCasePhraseCount: Int = 1

var phrase:String=camelCasePhrase


let lowercase: Set<Character> = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

phrase.removeAll(where: { lowercase.contains($0) })

camelCasePhraseCount=phrase.count

return camelCasePhraseCount + 1

}

//// Sample Input

var camelCasePhraseOne = "camelCasePhraseOne"

var camelCasePhraseTwo = "aMuchLongerCamelCasePhraseThatIsWayWayWayMoreFunToTest"

//

//// Sample Output

print(camelCaseWordCount(for: camelCasePhraseOne)) // 4

print(camelCaseWordCount(for: camelCasePhraseTwo)) // 15

Output

Add a comment
Know the answer?
Add Answer to:
having trouble with the following swift 5 problem. It is just printing the number 4 twice...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This looks long but it is easy just having some trouble please help out thank you....

    This looks long but it is easy just having some trouble please help out thank you. Find and fix all syntax and semantic errors which prevent the program from compiling. Find and fix all logical errors which cause the program to crash and/or produce unexpected results. In addition to making sure the code compiles, we have one final method which we need to complete. There is a method in the code, printAll, which is responsible for printing out the entirety...

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • Hello I need help with the following I'm having trouble getting to just the main code....

    Hello I need help with the following I'm having trouble getting to just the main code. Attached is the ButtonDebounce header file and the description of the code the language is C++; This lab will end with a functioning combination lock. The lock will have a combination that resides in EEPROM, and is programmable via the serial port. The user will enter a code, using the button and encoder on the board, and is shown on the lcd screen. Once...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

  • The skeleton code (starter file) for the problem is pasted below: def phoneNumber(letters): result = ""...

    The skeleton code (starter file) for the problem is pasted below: def phoneNumber(letters): result = "" # ADD YOUR CODE HERE return result def loadEvents(filename): events = {} # ADD YOUR CODE HERE return events def timeline(events): # ADD YOUR CODE HERE return # DO NOT modify or remove the code below! We will use it for testing. if __name__ == "__main__": # Problem 1: Decoding Telephone Numbers ph = input("Enter the text to translate: ") print("The corresponding phone number...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

  • Please help the out keeps printing twice #include #include #include #include #include using namespace std; /**...

    Please help the out keeps printing twice #include #include #include #include #include using namespace std; /** * Function to calculate the future value and return the same */ double calculateFutureValue(double presentValue, double interestRate, int months) {    double futureValue = (double)presentValue * pow((1 + interestRate), months);    return futureValue; } /** * Function to read the input file and assign the value to the variables */ unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months) {    inF >>...

  • Problem Statement In genetics, most large animals have two copies of every gene, one from each...

    Problem Statement In genetics, most large animals have two copies of every gene, one from each parent. In the simplest genetic model, each of the genes takes on one of two forms, usually represented by an uppercase and lowercase letter of the same value ('A' and 'a', for example). The pair of genes typically contributes to the external qualities of the animal in one of two ways. If both genes are uppercase, they contribute in one way, while if both...

  • Hello I'm having a bit of trouble with this assignment. Any help would be appreciated! Overview...

    Hello I'm having a bit of trouble with this assignment. Any help would be appreciated! Overview You must implement a Java class which simulates a Student object. These Student objects will represent grade records for students. Variable Details  name : private String : represents the Student’s name  sid: private String : represents the Student’s ID number  quizzes: private double[] : an array whose size is equal to NUM_QUIZZES. Each element will be used to store one of...

  • C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪...

    C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪ Writing a while loop ▪ Write functions and calling functions Text Processing [50 points] We would like to demonstrate our ability to control strings and use methods. There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT