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 start = word[0:][0]:

trans = input["enter a sentence "]
trans = trans.lower[]
t_list = trans.split[]
for word in t_list:
    print[word[0]]

Đ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 split[] chia không gian trắng], tiếp theo bạn bắt đầu thông qua danh sách đó [điều này trả về một ký tự duy nhất] và sau đó nhận được giá trị của ký tự đầu tiên của chuỗi.

Bạn cũng có thể sử dụng trans = input["enter a sentence "].lower[].split[] thay vì xác định lại trans thành một biến mới mỗi lần.

split[]86

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 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: //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++

#include

using namespace

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
1

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
4
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
5
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
8
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
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 : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9start = word[0:][0]0 start = word[0:][0]1start = word[0:][0]2start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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.
9start = word[0:][0]9 start = word[0:][0]0 split[]1start = word[0:][0]2 split[]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.
0start = word[0:][0]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.
9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

start = word[0:][0]4split[]9

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
9trans = input["enter a sentence "].lower[].split[]22

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.
9trans = input["enter a sentence "].lower[].split[]5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 trans0

trans = input["enter a sentence "].lower[].split[]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.
5 trans3

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans6trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3#include0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 #include3

trans = input["enter a sentence "].lower[].split[]5

Java

#include5 #include6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3#include9 using0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9using4
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
5
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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.
9using8
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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 namespace6namespace7namespace8

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 : hc
2

start = word[0:][0]4start = word[0:][0]0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
03start = word[0:][0]2start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]5

start = word[0:][0]4start = word[0:][0]9 start = word[0:][0]0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
17start = word[0:][0]2 split[]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.
0start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
25

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
9trans = input["enter a sentence "].lower[].split[]2
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]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.
9trans = input["enter a sentence "].lower[].split[]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.
9trans = input["enter a sentence "].lower[].split[]9 trans0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
42
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
43

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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 : hc
47trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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 : hc
51

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

trans = input["enter a sentence "].lower[].split[]5

Python 3

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
55
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
56
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
58

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
60
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
64
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
66

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
69
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
70
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
71
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 : hc
73
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 : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
76

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.
9start = 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 : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
81
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 start = word[0:][0]2
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
58

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
64
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
66

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 : hc
91
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 : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
94__

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
60
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 : hc
61
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 : hc
57
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

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
64
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
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 : hc
3trans = 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

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.
18
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
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 : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 trans7

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
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 : hc
57
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#

using

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

#include5 #include6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3#include9 using0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9using4
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
5
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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.
9using8
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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 namespace6namespace7namespace8

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 : hc
2

start = word[0:][0]4start = word[0:][0]0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
03start = word[0:][0]2start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]5

start = word[0:][0]4start = word[0:][0]9 start = word[0:][0]0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
17start = word[0:][0]2 split[]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.
0start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
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.
9trans = input["enter a sentence "].lower[].split[]9 trans0

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]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.
9trans = input["enter a sentence "].lower[].split[]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.
9trans = input["enter a sentence "].lower[].split[]9 trans0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
42
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
43

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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 : hc
47trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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.
9start = word[0:][0]08

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

trans = input["enter a sentence "].lower[].split[]5

Python 3

start = word[0:][0]12

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
55
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
56
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
58

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9start = word[0:][0]19
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
5
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

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.
9start = word[0:][0]23
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 : hc
6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
60
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

start = word[0:][0]4start = word[0:][0]0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
03start = word[0:][0]2start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
0
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]5

start = word[0:][0]4start = word[0:][0]9 start = word[0:][0]0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
17start = word[0:][0]2 split[]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.
0start = word[0:][0]3

start = word[0:][0]4

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
08
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 : hc
08
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
9trans = input["enter a sentence "].lower[].split[]2
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]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.
9trans = input["enter a sentence "].lower[].split[]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.
9trans = input["enter a sentence "].lower[].split[]9 trans0

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]70trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]73start = word[0:][0]74

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
42
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
43

Python 3 O[n]
Auxiliary space: O[n]. 

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
55
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
56
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
57
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
58
This approach uses the StringBuilder class of Java. In this approach, we will first split the input string based on the spaces. The spaces in the strings can be matched using a regular expression. The split strings are stored in an array of strings. Then we can simply append the first character of each split string in the String Builder object.  

Implementation:

C++

start = word[0:][0]76

using namespace

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
0

start = word[0:][0]80start = word[0:][0]81 start = word[0:][0]82

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]81 start = word[0:][0]86

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]88

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]90start = word[0:][0]91start = word[0:][0]92start = word[0:][0]93start = word[0:][0]94

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]96 start = word[0:][0]97

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9split[]01

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.
9start = word[0:][0]90start = word[0:][0]91split[]05start = word[0:][0]93start = word[0:][0]94

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]5

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3split[]11

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
3 split[]14

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.
9split[]16

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 split[]19

trans = input["enter a sentence "].lower[].split[]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.
5 trans3

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]81 split[]26trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3split[]30

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 #include3

trans = input["enter a sentence "].lower[].split[]5

Java

#include5 #include6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

split[]38split[]39 #include9 split[]41split[]42 split[]43

split[]38

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9 split[]47

split[]38

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9split[]51split[]52start = word[0:][0]94

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.
3split[]56

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 : hc
2

start = word[0:][0]4split[]60namespace7split[]62

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.
9trans = input["enter a sentence "].lower[].split[]5

start = word[0:][0]73trans = input["enter a sentence "].lower[].split[]9 split[]67

split[]38trans = input["enter a sentence "].lower[].split[]5

split[]38

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
42 split[]74

split[]38

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

start = word[0:][0]73split[]78split[]79

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

start = word[0:][0]73split[]82

split[]38trans = input["enter a sentence "].lower[].split[]5

trans = input["enter a sentence "].lower[].split[]5

Python3

split[]86

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 split[]88

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
55 split[]90split[]91
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
58

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3split[]94
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 split[]91split[]97start = word[0:][0]93start = word[0:][0]3

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
3 trans = input["enter a sentence "].lower[].split[]022____170 trans = input["enter a sentence "].lower[].split[]04

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.
9trans = input["enter a sentence "].lower[].split[]06namespace7trans = input["enter a sentence "].lower[].split[]08

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 trans = input["enter a sentence "].lower[].split[]11

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.
18
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 trans = input["enter a sentence "].lower[].split[]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.
22

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3split[]91
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 trans7

Các

C#

using

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

using trans = input["enter a sentence "].lower[].split[]34

#include5 #include6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

split[]38split[]39 #include9 split[]41split[]42 split[]43

split[]38

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9 split[]47

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

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.
9trans = input["enter a sentence "].lower[].split[]48start = word[0:][0]2start = word[0:][0]94

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.
9split[]51split[]52start = word[0:][0]94

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 : hc
2

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]59

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.
9trans = input["enter a sentence "].lower[].split[]5

start = word[0:][0]73trans = input["enter a sentence "].lower[].split[]9 split[]67

trans = input["enter a sentence "].lower[].split[]5

split[]38

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
40 #include9
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
42 split[]74

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3split[]78trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]76

trans = input["enter a sentence "].lower[].split[]5

trans = input["enter a sentence "].lower[].split[]5

split[]86
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 split[]88

start = word[0:][0]12

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
55 split[]90split[]91
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
58

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3split[]94
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
61 split[]91split[]97start = word[0:][0]93start = word[0:][0]3

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
2

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
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.
3 trans = input["enter a sentence "].lower[].split[]022____170 trans = input["enter a sentence "].lower[].split[]04

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.
9trans = input["enter a sentence "].lower[].split[]93

start = word[0:][0]4trans = input["enter a sentence "].lower[].split[]95

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.
9trans = input["enter a sentence "].lower[].split[]97

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 split[]19

trans = input["enter a sentence "].lower[].split[]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.
5 trans3

trans06

start = word[0:][0]75

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3start = word[0:][0]81 split[]26trans7
Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
6
O[n2]
Auxiliary space: O[n]. 

Input : str = "geeks for geeks"
Output : gfg

Input : str = "happy   coding"
Output : hc
3trans = input["enter a sentence "].lower[].split[]9 #include3: Using boundary checker, refer //www.geeksforgeeks.org/get-first-letter-word-string-using-regex-java/

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.

Chủ Đề