Return the number of times a word appears in a string

  • Comp Sci
  • Thread starter ver_mathstats
  • Start date
  • Tags
    String
In summary: I completely agree. I think it's important to get an accurate answer, but I think it's just as important to understand the concepts behind it.
  • #1
ver_mathstats
260
21
Homework Statement
Return the number of times that the string "code" appears anywhere in a given string
Relevant Equations
Using python
Python:
def count_code(str):
       count = 0
       for c in range(len(str)-1):
              if str[c] == 'c' and str[c+1] == 'o' and str[c+2] == 'd' and str[c+3] == 'e':
                          count+=1
              return count
Here is my code so far, I did use some test results with print statements but nothing comes up when I run my code and I am unsure of where I am going wrong.

Any help would be greatly appreciated. Thank you.
<mentor add code tags>
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I would think the term str[c+3] would give an index range error when c points at the 2nd and 3rd last chars of the string. That could be something to do with why the results are not what expected. Only let c run up to the fourth-last character.

Also, you have no indentation for the function, the for statement or the if statement. It won't work without indentation. If the latter is an artefact of formatting on this website, enclose your code within delimiters [code] and [/code] when posting on here so the indentation does not disappear.
 
  • #3
Yes I realized some mistakes and fixed it. Thank you. And yes I am sorry about that I do have it indented in python, I just did not know how exactly to format it on PhysicsForum but thank you, will use that now. Thank you for the help.
 
  • #4
Use code tags - they look like this with no spaces:
[ c o d e = python ]
... code lines here ...
[ / c o d e ]
 
  • #5
jim mcnamara said:
Use code tags - they look like this with no spaces:
[ c o d e = python ]
... code lines here ...
[ / c o d e ]
Yes thank you. Greatly appreciated.
 
  • #6
ver_mathstats said:
Homework Statement:: Return the number of times that the string "code" appears anywhere in a given string
Relevant Equations:: Using python

Python:
def count_code(str):
       count = 0
       for c in range(len(str)-1):
              if str[c] == 'c' and str[c+1] == 'o' and str[c+2] == 'd' and str[c+3] == 'e':
                          count+=1
              return count
Here is my code so far, I did use some test results with print statements but nothing comes up when I run my code and I am unsure of where I am going wrong.

Any help would be greatly appreciated. Thank you.
<mentor add code tags>
I don't see a good reason in this instance for your not presenting the whole script.

If it were up to me, I'd code it as:
Python:
givenstring = 'code some code and then code some more'
print givenstring.count('code')
but I'm not confident that your instructor wouldn't take a dim view of your using the .count shortcut. :wink:
 
  • Like
Likes jim mcnamara
  • #7
The return count statement is too much indented. The function will return the first time around the for loop, so it can only return 1 if the input starts with 'code' and 0 otherwise..
 
  • Like
Likes ver_mathstats and FactChecker
  • #8
sysprog said:
but I'm not confident that your instructor wouldn't take a dim view of your using the .count shortcut. :wink:
I agree. There is a lot of general programming that this exercise teaches. In any case, there are functions for using regular expressions that, IMHO, one should eventually learn.
 
  • Like
Likes ver_mathstats, sysprog and jim mcnamara
  • #9
I second @FactChecker comment about learning REGEX (regular expressions)
 
  • Like
Likes ver_mathstats and sysprog
  • #10
In python a ##\mathtt{\text{regex}}## command (not just ##\mathtt{\text{re}}## ) can be configured to allow recursion and to use an extenal stack. Technically that makes it a context-free grammar instead of properly or strictly regular.

Even so, it's helpful to have the regex iterator in the Boost library if you want to get all instances of a substring in a string -- https://www.boost.org/doc/libs/1_72_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html
 
  • Like
Likes ver_mathstats
  • #11
sysprog said:
I don't see a good reason in this instance for your not presenting the whole script.

If it were up to me, I'd code it as:
Python:
givenstring = 'code some code and then code some more'
print givenstring.count('code')
but I'm not confident that your instructor wouldn't take a dim view of your using the .count shortcut. :wink:
Sorry, we're asked to do it in a specific way with concepts we had learned the previous week. Thank you for the help though.
 
  • Informative
Likes sysprog
  • #12
ver_mathstats said:
Sorry, we're asked to do it in a specific way with concepts we had learned the previous week. Thank you for the help though.
That seems right to me. Getting a correct answer isn't all that you're seeking. It's important to build the concepts and skills.
 
  • Like
Likes FactChecker and jim mcnamara

Related to Return the number of times a word appears in a string

1. How can I return the number of times a specific word appears in a string?

To return the number of times a word appears in a string, you can use a loop to iterate through the string and count the number of times the word appears. You can also use built-in string methods, such as the count() method in Python, to count the occurrences of a specific word.

2. Can I return the number of times multiple words appear in a string?

Yes, you can return the number of times multiple words appear in a string by using a similar approach as mentioned in the first question. You can either use a loop or built-in string methods to count the occurrences of each word and then add them together to get the total count.

3. How do I handle case sensitivity when counting word occurrences in a string?

To handle case sensitivity when counting word occurrences in a string, you can either convert the string to lowercase or uppercase before counting the occurrences. This ensures that the count is not affected by the case of the words in the string.

4. Is there a way to return the number of times a word appears in a string without using loops?

Yes, there are built-in methods or functions in many programming languages that allow you to count the occurrences of a word in a string without using loops. For example, in Python, you can use the count() method or the collections.Counter() function to count word occurrences in a string.

5. Can I return the number of times a word appears in a string if the word is part of a larger word?

Yes, you can return the number of times a word appears in a string even if the word is part of a larger word. For example, if you want to count how many times the word "the" appears in a string, it will also count words like "there" or "they" as long as "the" is part of the word.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
973
  • Engineering and Comp Sci Homework Help
Replies
4
Views
906
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
900
  • Programming and Computer Science
2
Replies
40
Views
2K
Back
Top