Monday, July 6, 2020

Capitalize!-HackerRank Solution

 Capitalize!-HackerRank Solution

 You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck.

Given a full name, your task is to capitalize the name appropriately.


Input Format

A single line of input containing the full name, .

Constraints

  • The string consists of alphanumeric characters and spaces.

Note: in a word only the first character is capitalized. Example 12abc when capitalized remains 12abc.

Output Format

Print the capitalized string, .

Sample Input

chris alan

Sample Output

Chris Alan
Capitalize Solution-
l=[]
def solve(s):
    x=re.split("\s",s) #store the string by seperating them in words in a list
    for i in x:
        c=str(i)
        out=c.capitalize() #capitalize the first character of each word
        l.append(out)
    return ((" ").join(l))    #join the list by adding space between each string
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    s = input()
    result = solve(s)
    fptr.write(result + '\n')
    fptr.close()
If you want to read more about how capitalize function works than follow this link-https://www.geeksforgeeks.org/string-capitalize-python/
In order to capitalize the first character of the string, we can also use camelcase module. One short example is shown below-
import camelcase
c=camelcase.CamelCase()
str="convert to uppercase"
print(c.hump(str))

Output- Convert To Uppercase

0 comments:

Post a Comment

Feel free to share your thoughts and suggestion...