#Counting characters in a string homework

1 messages · Page 1 of 1 (latest)

nova pawn
#

I honestly don't even know where to start with this. I know I'm supposed to incorporate loops in this, but I'm unsure how to approach this. I would appreciate it if someone could walk me through the logic behind this without giving me the answer as I want to be able to solve this myself.

My thought process is:

  1. Count the length of the string. (totalLength)
  2. Count the length of the string without the specified character (notCharLength)
  3. totalLength - notCharLength = answer
  4. Then use if else statements to print the output

The problem I'm running into is that I don't know how to distinguish specific characters from inside the string.

lament jettyBOT
#

<@&987246399047479336> please have a look, thanks.

lament jettyBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

some guidance or a starting point.

To count the characters in a string using loops, you can follow these steps:

  1. Declare a variable to store the count of characters and initialize it to 0.
  2. Prompt the user to enter a string.
  3. Use a loop (such as a for loop or while loop) to iterate through each character in the string.
  4. For each character, increment the count variable by 1.
  5. After the loop finishes, display the count of characters.

Here's an example implementation in Python:

# Step 1: Initialize count variable
count = 0

# Step 2: Prompt user for input
string = input("Enter a string: ")

# Step 3: Loop through each character
for char in string:
    # Step 4: Increment count variable
    count += 1

# Step 5: Display result
print("The number of characters is:", count)

In this example, we use a for loop to iterate through each character in the string variable. For each character, we increment the count variable by 1. Finally, we display the result using print.

You can run this code and test it with different strings to see how it counts the characters correctly.

Remember that this is just one approach, and there are multiple ways to solve this problem using loops. Feel free to explore different variations or optimizations based on your requirements.

azure canyon
#
  1. You don't need the length of input string(you could use it for iteration tho)
  2. You need to count the no of times a character appears in a string
  3. To count that, you need to check each character in the string
nova pawn
#

Ok, thank you. I will try it again right now 🙂

umbral eagle
#

if you want a fun solution on this, you could create a new string using the first string, regex off the char you want to count, then compare the two's lengths

paper juniper
#

a simple solution to this could be to use IntStream's java public static long occurrenceOf(String str, char character){ return str.chars().filter(ch -> ch == character).count(); }

lament jettyBOT