I want to Take Out Each word from String, Please Help

Guys i want to take out each word from one sentence in string.

my string is this :
str1[100] = {"Sun Moon Earth");

I can take out first Word Sun, but another words i cant.

my code is here, please help me if u can, im beginner.


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
#include <iostream>
#include <string.h>
#include <sstream>

using namespace std;


int main()
{
    char str1[100] = {"Sun Moon Earth"};
    char str2[100];
    char str3[100];


    int i = 0;

    while(str1[i] != ' ')
    {
        str2[i] = str1[i];
        i++;
    }

    cout << str2 << endl;








    cin.get();
    cin.get();
    return 0;
}
Last edited on
There is a function for this :

strtok ( str , delimiters );

in your case : strtok( str1 , ' ')

You can use it in a while loop to cout each word to avoid that str2.

for exemple :

char *pointer;
pointer = strtok (str1 ,' ');
while(pointer!=NULL)
{ cout<<pointer;
pointer = strtok (str1 ,' ');
}


I hope that this helped you
Last edited on
There are a couple of issues here so I'll do my best to sort them out. First, in c++ programs, it's strongly advised to not use "C strings". These are the strings you're referring to by declaring str1[100] as a char type. You should instead use a std::string, like this:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
    std::string str1 = "Sun Moon Earth";
    std::cout << str1 << std::endl;
    return 0;
}


Now, next you say you want to extract those words (presumably to different strings). Take a look at this solution:

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
#include <iostream>
#include <string>

int main()
{
    std::string str1 = "Sun Moon Earth";
    std::string str2;
    std::string str3;
    std::string str4;

    size_t foundSpacePosition = str1.find(" "); //std::string::find() returns the position of the first found instance of the string you're passing it, which is in this case the space.

    str2 = str1.substr(0, foundSpacePosition); //std::string::substr returns a substring from the first argument to the second argument.
    //In this case, we want a substring from the 0th position to the found position of the space.
    
    std::string tempString = str1.substr(foundSpacePosition+1); //If you only pass one argument to substr(), it will return a substring from that position to the end of the string
    //Because std::string::find() only finds the first instance of the character, we must remove it to continue.
    //We will make a copy of this so as to not modify the original str1, saving it into tempString

    foundSpacePosition = tempString.find(" "); //Find the next space
    str3 = tempString.substr(0, foundSpacePosition); //Same as before

    tempString = tempString.substr(foundSpacePosition+1); //Same as before 
    
    foundSpacePosition = tempString.find(" "); //Same as before
    str4 = tempString.substr(0); //Since there is nothing else left in this string,
    //we can just go from the 0th position to the end

    std::cout << "str1 = " << str1 << std::endl;
    std::cout << "str2 = " << str2 << std::endl;
    std::cout << "str3 = " << str3 << std::endl;
    std::cout << "str4 = " << str4 << std::endl;
    return 0;
}


And the output:

1
2
3
4
str1 = Sun Moon Earth
str2 = Sun
str3 = Moon
str4 = Earth
Last edited on
Topic archived. No new replies allowed.