Karan Thakkar
Karan Thakkar

Reputation: 1027

Counting words starting with a character

Write a function that accepts a string and a character as input and returns the count of all the words in the string which start with the given character. Assume that capitalization does not matter here. You can assume that the input string is a sentence i.e. words are separated by spaces and consists of alphabetic characters.

This is my code:

def count_input_character (input_str, character):
    input_str = input_str.lower()
    character = character.lower()
    count = 0
    for i in range (0, len(input_str)):
        if (input_str[i] == character and input_str[i - 1] == " "):
            count += 1
    return (count)



#Main Program
input_str = input("Enter a string: ")
character = input("Enter character whose occurances are to be found in the given input string: ")
result = count_input_character(input_str, character)
#print(result)

The only part missing here is that how to check if the first word of the sentence is stating with the user given character. consider this output:

Your answer is NOT CORRECT Your code was tested with different inputs. > For example when your function is called as shown below:

count_input_character ('the brahman the master of the universe', 't')

####### Your function returns ############# 2 The returned variable type is: type 'int' ### Correct return value should be ######## 3 The returned variable type is: type 'int'

Upvotes: 1

Views: 4290

Answers (4)

axpixix
axpixix

Reputation: 1

def c_upper(text, char):
   text = text.title() #set leading char of words to uppercase
   char = char.upper() #set given char to uppercase
   k = 0 #counter
   for i in text:
     if i.istitle() and i == char: #checking conditions for problem, where i is a char in a given string
        k = k + 1
   return k

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61512

Instead of looking for spaces, you could split input_str on whitespace, this would produce a list of words that you could then test against character. (Pseudocode below)

function F sentence, character {
    l = <sentence split by whitespace> 

    count = 0
    for word in l { 
       if firstchar(word) == character { 
          count = count + 1
       }   
    }

    return count
}

Upvotes: 2

tobias_k
tobias_k

Reputation: 82899

You function misses the first t because in this line

if (input_str[i] == character and input_str[i - 1] == " "):

when i is 0, then input_str[i - 1] is input_str[-1] which Python will resolve as the last character of the string!

To fix this, you could change your condition to

if input_str[i] == character and (i == 0 or input_str[i - 1] == " "):

Or use str.split with a list comprehension. Or a regular expression like r'(?i)\b%s', with (?i) meaning "ignore case", \b is word boundary and %s a placeholder for the character..

Upvotes: 4

Alex
Alex

Reputation: 21766

Although it doesn't fix your specific bug, for educational purposes, please note you could rewrite your function like this using list comprehension:

def count_input_character (input_str, character):
    return len([x for x in input_str.lower().split() if x.startswith(character.lower())])

or even more efficiently(thanks to tobias_k)

def count_input_character (input_str, character):
    sum(w.startswith(character.lower()) for w in input_str.lower().split())

Upvotes: 1

Related Questions