Hướng dẫn how do you find the first letter of a word in a string in python? - Làm thế nào để bạn tìm thấy chữ cái đầu tiên của một từ trong một chuỗi trong python?
Làm thế nào về bạn chỉ cần cắt chuỗi khi in (hoặc gán một biến mới) và xóa Show
Điều này hoạt động bởi vì bạn nhận được một danh sách chứa tất cả các giá trị của chuỗi (theo mặc định Bạn cũng có thể sử dụng
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 split() 88str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space.Examples: Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc Nguồn: https://www.geeksforgeek.org/amazon-interview-set-8-2/ Ý tưởng là đi qua từng ký tự của Str Str và duy trì một biến boolean, ban đầu được đặt là đúng. Bất cứ khi nào chúng tôi gặp không gian, chúng tôi đặt biến Boolean là đúng. Và nếu chúng ta gặp bất kỳ ký tự nào khác ngoài không gian, chúng ta sẽ kiểm tra biến Boolean, nếu nó được đặt đúng như bản sao điều lệ cho chuỗi đầu ra và đặt biến Boolean là sai. Nếu biến boolean được đặt sai, không làm gì cả. & Nbsp; Algorithm: 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing. Implementation: C++
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc1 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc4 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc5 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc8 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.5 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 0 start = word[0:][0] 1start = word[0:][0] 2start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 9 start = word[0:][0] 0 split() 1start = word[0:][0] 2 split() 31. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 start = word[0:][0] 31. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 trans = input("enter a sentence ").lower().split() 221. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 5Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 trans 0
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.5 trans 3Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans 6trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 #include 0Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 #include 3
Java
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 #include 9 using 0Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 using 4Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc5 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 using 8 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.5 namespace 6namespace 7namespace 81. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc03 start = word[0:][0] 2start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc17 start = word[0:][0] 2 split() 31. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc25 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 trans = input("enter a sentence ").lower().split() 2Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 51. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 9 trans 0Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc42 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc43 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc47 trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc51 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5
Python 3Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc55 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc56 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc58 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc60 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc5 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc64 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc66 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc69 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc70 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc71 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc73 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc76 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 0 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc81 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 start = word[0:][0] 2Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc58
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc64 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc66 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc91 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc94__
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc60 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.05 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.09
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc64 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.13 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.16
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.18 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.21 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.22 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.28 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.29 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.31 C#
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.33
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 #include 9 using 0Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 using 4Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc5 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 using 8 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.4 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.5 namespace 6namespace 7namespace 81. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc03 start = word[0:][0] 2start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc17 start = word[0:][0] 2 split() 31. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.82 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 9 trans 0
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 51. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 9 trans 0Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc42 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc43 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc47 trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 08Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5
Python 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc55 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc56 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc57 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc58 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 19Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc5 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 231. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc60 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc5 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc03 start = word[0:][0] 2start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc17 start = word[0:][0] 2 split() 31. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.0 start = word[0:][0] 3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.82 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc08 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc9 trans = input("enter a sentence ").lower().split() 2Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 51. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 9 trans 0Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 start = word[0:][0] 70trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc42 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc43 Python 3 O(n)
Implementation: C++
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc0
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 start = word[0:][0] 81 start = word[0:][0] 86Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 start = word[0:][0] 88Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 start = word[0:][0] 90start = word[0:][0] 91start = word[0:][0] 92start = word[0:][0] 93start = word[0:][0] 94Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 start = word[0:][0] 96 start = word[0:][0] 97Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 split() 011. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 start = word[0:][0] 90start = word[0:][0] 91split() 05start = word[0:][0] 93start = word[0:][0] 94Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 5Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 split() 11
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 split() 141. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 split() 16Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 split() 19
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.5 trans 3Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 start = word[0:][0] 81 split() 26trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 split() 30Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 #include 3
Java
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 split() 47
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 split() 51split() 52start = word[0:][0] 941. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 split() 561. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 5
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc42 split() 74
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6
Python3
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 split() 88Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc55 split() 90split() 91Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc58 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 split() 94Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 split() 91split() 97start = word[0:][0] 93start = word[0:][0] 3Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 trans = input("enter a sentence ").lower().split() 022____170 trans = input("enter a sentence ").lower().split() 041. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 06namespace 7trans = input("enter a sentence ").lower().split() 08Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 trans = input("enter a sentence ").lower().split() 11
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.18 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 trans = input("enter a sentence ").lower().split() 161. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.22 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 split() 91 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 trans 7Các C#
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.33
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 split() 47Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 48start = word[0:][0] 2start = word[0:][0] 941. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 split() 51split() 52start = word[0:][0] 941. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 5
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc40 #include 9 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc42 split() 74Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 split() 78trans 7Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc6 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 76
split()86Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 split()88
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc55 split() 90split() 91Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc58 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 split() 94Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc61 split() 91split() 97start = word[0:][0] 93start = word[0:][0] 3Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc2 Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.3 trans = input("enter a sentence ").lower().split() 022____170 trans = input("enter a sentence ").lower().split() 041. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 93
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.9 trans = input("enter a sentence ").lower().split() 97Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hc3 trans = input("enter a sentence ").lower().split() 9 split() 19
1. Traverse string str. And initialize a variable v as true. 2. If str[i] == ' '. Set v as true. 3. If str[i] != ' '. Check if v is true or not. a) If true, copy str[i] to output string and set v as false. b) If false, do nothing.5 trans 3
JavaAarti_Rathi and Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Làm thế nào để bạn có được chữ cái đầu tiên của mỗi từ trong một chuỗi?Để lấy chữ cái đầu tiên của mỗi từ trong một chuỗi: gọi phương thức chia () trên chuỗi để lấy một mảng chứa các từ trong chuỗi.Gọi phương thức Bản đồ () để lặp qua mảng và trả lại chữ cái đầu tiên của mỗi từ.Tham gia mảng của các chữ cái đầu tiên vào một chuỗi, sử dụng phương thức Jop ().
Làm thế nào để tôi tìm thấy sự xuất hiện đầu tiên của một ký tự trong một chuỗi trong Python?Để tìm vị trí xuất hiện đầu tiên của chuỗi, bạn có thể sử dụng chuỗi.tìm () phương thức.
Làm thế nào để bạn có được chữ cái đầu tiên của mỗi từ trong một python thủ đô?Chuỗi viết hoa () trong python python chuỗi viết tắt () Phương thức trả về một bản sao của chuỗi gốc và chuyển đổi ký tự đầu tiên của chuỗi thành chữ cái (chữ hoa), trong khi tạo tất cả các ký tự khác trong các chữ cái viết thường. in Python
Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter, while making all other characters in the string lowercase letters. |