Sentence for word string

I wanted to know how to iterate through a string word by word.

string = "this is a string"
for word in string:
    print (word)

The above gives an output:

t
h
i
s

i
s

a

s
t
r
i
n
g

But I am looking for the following output:

this
is
a
string

Pavel's user avatar

Pavel

5,0804 gold badges29 silver badges53 bronze badges

asked Aug 6, 2015 at 1:41

m0bi5's user avatar

1

When you do —

for word in string:

You are not iterating through the words in the string, you are iterating through the characters in the string. To iterate through the words, you would first need to split the string into words , using str.split() , and then iterate through that . Example —

my_string = "this is a string"
for word in my_string.split():
    print (word)

Please note, str.split() , without passing any arguments splits by all whitespaces (space, multiple spaces, tab, newlines, etc).

Olivier Pons's user avatar

Olivier Pons

15.3k26 gold badges118 silver badges211 bronze badges

answered Aug 6, 2015 at 1:43

Anand S Kumar's user avatar

Anand S KumarAnand S Kumar

87.3k18 gold badges183 silver badges172 bronze badges

2

This is one way to do it:

string = "this is a string"
ssplit = string.split()
for word in ssplit:
    print (word)

Output:

this
is
a
string

answered Aug 6, 2015 at 1:45

Joe T. Boka's user avatar

Joe T. BokaJoe T. Boka

6,5046 gold badges27 silver badges48 bronze badges

for word in string.split():
    print word

answered Aug 6, 2015 at 1:50

Connor's user avatar

ConnorConnor

1346 bronze badges

2

Using nltk.

from nltk.tokenize import sent_tokenize, word_tokenize
sentences = sent_tokenize("This is a string.")
words_in_each_sentence = word_tokenize(sentences)

You may use TweetTokenizer for parsing casual text with emoticons and such.

answered Oct 16, 2019 at 21:24

noɥʇʎԀʎzɐɹƆ's user avatar

noɥʇʎԀʎzɐɹƆnoɥʇʎԀʎzɐɹƆ

9,6992 gold badges47 silver badges65 bronze badges

One way to do this is using a dictionary. The problem for the code above is it counts each letter in a string, instead of each word. To solve this problem, you should first turn the string into a list by using the split() method, and then create a variable counts each comma in the list as its own value. The code below returns each time a word appears in a string in the form of a dictionary.

    s = input('Enter a string to see if strings are repeated: ')
    d = dict()
    p = s.split()
    word = ','
    for word in p:
        if word not in d:
            d[word] = 1
        else:
            d[word] += 1
    print (d)

answered Nov 17, 2021 at 2:55

The_Chosen_One69's user avatar

s = 'hi how are you'
l = list(map(lambda x: x,s.split()))
print(l)

Output: ['hi', 'how', 'are', 'you']

answered Dec 11, 2019 at 15:56

Nanda Thota's user avatar

Nanda ThotaNanda Thota

3023 silver badges10 bronze badges

You can try this method also:

sentence_1 = «This is a string»

list = sentence_1.split()

for i in list:

print (i)

answered Aug 9, 2022 at 9:31

Samartha Chakrawarti's user avatar

2

There are a few things of interest with your solution. Firstly, it is a very literal implementation of the problem to solve, and I worry that it is too literal. For example, are you sure that sentences end with just a period .? Is it not a period and some whitespace? Is a URL like example.com two sentences?

The second issue I have is the blind trust you have in the inputs. You happily convert the input text to lower-case (too often, actually), but you do not convert the word to lower-case. If someone gives an upper-case word, you’ll never find it.

I would prefer a more zen approach, using regular expressions… actually, just a split, and some Java 8 niceness.

private static final Pattern END_OF_SENTENCE = Pattern.compile("\.\s+");

public static String getSentence(String text, String word) {
    final String lcword = word.toLowerCase();
    return END_OF_SENTENCE.splitAsStream(text)
            .filter(s -> s.toLowerCase().contains(lcword))
            .findAny()
            .orElse(null);
}

Why is that better? Well, it streams the text in the form of sentences, and then finds the first match in a sentence. If there are no sentences, it matches the whole thing.

Note that the same principles can be used with a non-streaming approach. Split by sentences, then find the first match.

In an android environment, you could do:

private static final Pattern END_OF_SENTENCE = Pattern.compile("\.\s+");

public static String getSentence(String text, String word) {
    final String lcword = word.toLowerCase();
    for (String sentence : END_OF_SENTENCE.split(text)) {
        if (sentence.toLowerCase().contains(lcword)) {
            return sentence;
        }
    }
    return null;
}

Note that the results from the above code may, or may not include the terminating period. If the match is in the last sentence of a text, and that text ends with a period, then the period may be returned as part of the result. If there is a match in the middle of the text, then the period will not be included.

Synonym: bind, connect, line up, thread, tie. Antonym: unstring. Similar words: striking, strip, strike, strict, doctrine, restrict, district, strictly. Meaning: [strɪŋ]  n. 1. a lightweight cord 2. stringed instruments that are played with a bow 3. a tightly stretched cord of wire or gut, which makes sound when plucked, struck, or bowed 4. a sequentially ordered set of things or events or ideas in which each successive member is related to the preceding 5. a linear sequence of symbols (characters or words or phrases) 6. a tie consisting of a cord that goes through a seam around an opening 7. (cosmology) a hypothetical one-dimensional subatomic particle having a concentration of energy and the dynamic properties of a flexible loop 8. a collection of objects threaded on a single strand 9. a necklace made by a stringing objects together. v. 1. thread on or as if on a string 2. add as if on a string 3. move or come along 4. stretch out or arrange like a string 5. string together; tie or fasten with a string 6. remove the stringy parts of 7. provide with strings. 

Random good picture Not show

1. Where the knot is loose the string slips. 

2. She undid the string round the parcel.

3. Jean wore a string of pearls around her neck.

4. The books were tied with string.

5. He twanged the guitar string/his braces.

6. I snipped the string and untied the parcel.

7. Give me that roll of string for my kite.

8. Make a knot at the end of the string.

9. The string around the parcel was slack.

10. A string quartet was playing Mozart.

11. A string of scandals is obsessing America.

12. Dan-gle the ball on a string before the baby.

13. Could you roll up that string for me?

14. He made the second string on the football team.

15. Her key hung on a string around her neck.

16. The string is wound round a cardboard cylinder.

17. Could you tie this piece of string for me?

18. The parcel was tied with string.

19. We used a razor to cut the string.

20. He pulled the string tight.

21. to harp on the same string.

22. String is made of threads twisted together.

23. He jerked the string and the puppet jumped.

24. The key is hanging on a string by the door.

25. I need a piece of string to tie this parcel up.

26. A string quartet consists of two violins[sentencedict.com], a viola and a cello.

27. We’ll need several lengths of string to tie the package.

27. Sentencedict.com try its best to collect and make good sentences.

28. Using the right hand to touch the string gently through the fingers,the trace of lubrication,with my flying fantasy.

29. Take my advice – string along with me, I know this business inside out.

30. She took a pair of scissors and cut the string.

More similar words: striking, strip, strike, strict, doctrine, restrict, district, strictly, distribute, industrial, ring, restriction, distribution, The last rites, bring, during, ring out, ring up, bring in, bring out, mothering, offering, bring up, gathering, bring down, spring up, bearings, bring about, filtering, bring back. 

Java split String by words example shows how to split string into words in Java. The example also shows how to break string sentences into words using the split method.

The simplest way to split the string by words is by the space character as shown in the below example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

package com.javacodeexamples.stringexamples;

import java.util.Arrays;

public class StringSplitByWords {

    public static void main(String[] args) {

        String sentence = «Java String split by words from sentence»;

        //get words from sentence

        String[] words = splitSentenceByWords(sentence);

        //print words

        System.out.println(Arrays.toString(words));

    }

    private static String[] splitSentenceByWords(String str){        

        //if string is empty or null, return empty array

        if(str == null || str.equals(«»))

            return new String[0];

        String[] words = str.split(» «);

        return words;        

    }

}

Output

[Java, String, split, by, words, from, sentence]

As you can see from the output, it worked for the test sentence string. The sentence is broken down into words by splitting it using space.

Let’s try some other not-so-simple sentences.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.javacodeexamples.stringexamples;

import java.util.Arrays;

public class StringSplitByWords {

    public static void main(String[] args) {

        String[] sentences = {

                «string  with    lot of   spaces»,

                «Hello, can I help you?»,

                «Java is a ‘programming’ language.»,

                «this is user-generated content»

                };

        for (String sentence : sentences){

            //get words from sentence

            String[] words = splitSentenceByWords(sentence);

            //print words

            System.out.println(Arrays.toString(words));            

        }

    }

    private static String[] splitSentenceByWords(String str){        

        //if string is empty or null, return empty array

        if(str == null || str.equals(«»))

            return new String[0];

        String[] words = str.split(» «);

        return words;        

    }

}

Output

[string, , with, , , , lot, of, , , spaces]

[Hello,, can, I, help, you?]

[Java, is, a, ‘programming’, language.]

[this, is, user-generated, content]

As you can see from the output, our code did not work as expected. The reason being is simple split by space is not enough to separate words from a string. Sentences may be separated by punctuation marks like dot, comma, question marks, etc.

In order to make the code handle all these punctuation and symbols, we will change our regular expression pattern from only space to all the punctuation marks and symbols as given below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.javacodeexamples.stringexamples;

import java.util.Arrays;

public class StringSplitByWords {

    public static void main(String[] args) {

        String[] sentences = {

                «string  with    lot of   spaces»,

                «Hello, can I help you?»,

                «Java is a ‘programming’ language.»,

                «this is [user-generated] content»

                };

        for (String sentence : sentences){

            //get words from sentence

            String[] words = splitSentenceByWords(sentence);

            //print words

            System.out.println(Arrays.toString(words));            

        }

    }

    private static String[] splitSentenceByWords(String str){        

        //if string is empty or null, return empty array

        if(str == null || str.equals(«»))

            return new String[0];

        String[] words = str.split(«[ !»\#$%&'()*+,-./:;<=>[email protected]\[\]^_`{|}~]+»);

        return words;        

    }

}

Output

[string, with, lot, of, spaces]

[Hello, can, I, help, you]

[Java, is, a, programming, language]

[this, is, user, generated, content]

This time we got the output as we wanted. The regex pattern [ !"\#$%&'()*+,-./:;<=>[email protected]\[\]^_`{|}~]+ includes almost all the punctuation and symbols that can be used in a sentence including space. We applied + at the end to match one or more instances of these to make sure that we do not get any empty words.

Instead of this pattern, you can also use \P{L} pattern to extract words from the sentence, where \P denotes POSIX expression and L denotes character class for word characters. You need to change the line with the split method as given below.

String[] words = str.split(«\P{L}+»);

Please note that \P{L} expression works for both ASCII and non-ASCII characters (i.e. accented characters like “café” or “kākā”).

This example is a part of the Java String tutorial with examples and the Java RegEx tutorial with examples.

Please let me know your views in the comments section below.

About the author

  • Author
  • Recent Posts

Rahim

I have a master’s degree in computer science and over 18 years of experience designing and developing Java applications. I have worked with many fortune 500 companies as an eCommerce Architect. Follow me on LinkedIn and Facebook.

Give a sentence, print different words present in it. Words are separated by space. 

Examples: 

Input : str = "Geeks for Geeks"
Output : Geeks
         for
         Geeks  
Explanation : All space separated words 
are printed line by line.

Input : str = "a computer science portal"
Output : a
         computer
         science
         portal

Method 1 (Writing our own logic):

We traverse through all characters. If current character is space, we have reached end of a word, we print current word and reset it to empty. Else we append current character to word. 

Implementation:

CPP

#include <bits/stdc++.h>

using namespace std;

void removeDupWord(string str)

{

    string word = "";

    for (auto x : str)

    {

        if (x == ' ')

        {

            cout << word << endl;

            word = "";

        }

        else {

            word = word + x;

        }

    }

    cout << word << endl;

}

int main()

{

    string str = "Geeks for Geeks";

    removeDupWord(str);

    return 0;

}

Complexity Analysis:

  • Time complexity : O(n) 
  • Auxiliary Space : O(n)

Method 2 (Using strtok()):

Implementation:

CPP

#include <bits/stdc++.h>

using namespace std;

void removeDupWord(char str[])

{

    char *token = strtok(str, " ");

    while (token != NULL)

    {

        printf("%sn", token);

        token = strtok(NULL, " ");

    }

}

int main()

{

    char str[] = "Geeks for Geeks";

    removeDupWord(str);

    return 0;

}

Note: strtok() function cannot be used with C++ STL string. It requires string to be character array.

Complexity Analysis:

  • Time complexity : O(n) 
  • Auxiliary Space : O(n)

Method 3 (Using stringstream):

Implementation:

CPP

#include <bits/stdc++.h>

using namespace std;

void removeDupWord(string str)

{

    istringstream ss(str);

    string word;

    while (ss >> word)

    {

        cout << word << "n";

    }

}

int main()

{

    string str = "Geeks for Geeks";

    removeDupWord(str);

    return 0;

}

Time complexity : O(n) 
Auxiliary Space : O(n)

Method 4 (Using std::getline() Function):

The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header.

Implementation

C++

#include <iostream>

#include <sstream>

#include <string>

int main()

{

    std::string sentence = "Geeks For Geeks";

    std::string word;

    std::istringstream iss(sentence);

    while (std::getline(iss, word, ' ')) {

        std::cout << word << std::endl;

    }

    return 0;

}

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1).

Method 5 (Using Temporary String):

In C++, one approach is to use a temporary string to hold each word as it is extracted from the sentence. The sentence can be split into words by iterating over each character in the sentence and checking for whitespace characters. When a whitespace character is encountered, the temporary string is considered a word and can be added to a list of words. This process is repeated until all characters in the sentence have been processed.

Implementation

C++

#include <iostream>

#include <string>

#include <vector>

int main() {

    std::string sentence = "Geeks For Geeks";

    std::vector<std::string> words;

    std::string tempWord;

    for (char c : sentence) {

        if (isspace(c)) {

            if (!tempWord.empty()) {

                words.push_back(tempWord);

                tempWord.clear();

            }

        } else {

            tempWord.push_back(c);

        }

    }

    if (!tempWord.empty()) {

        words.push_back(tempWord);

    }

    for (std::string word : words) {

        std::cout << word << std::endl;

    }

    return 0;

}

Time Complexity: O(N), where N is the length of the sentence.
Auxiliary Space: O(N)

Method 6 (Using find(), substr() and erase() Functions):

One approach is to use the find(), substr() and erase() functions to split a sentence into words in C++. 

Steps:

  1. Initialises a string with the sentence and declares two variables for the position and the extracted word.
  2. It then uses a while loop to find the spaces in the sentence and extract the words using the substr() function. 
  3. Lastly, it prints out the last word of the sentence.

Implementation

C++

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string sentence = "Geeks For Geeks";

    size_t pos = 0;

    string word;

    while ((pos = sentence.find(" ")) != string::npos) {

        word = sentence.substr(0, pos);

        cout << word << endl;

        sentence.erase(0, pos + 1);

    }

    cout << sentence;

    return 0;

}

Time Complexity: O(N), where N is the length of the sentence.
Auxiliary Space: O(1)

Понравилась статья? Поделить с друзьями:
  • Sentence for word rode
  • Sentence for word imperative
  • Sentence for the word available
  • Sentence for the word author
  • Sentence for the word arrive