Hello...I need to write a program where i need to copy each word of a string into an array...
Eg(string s="hello how are you" arr[0]=hello,arr[1]=how....).
any help:?: ??

Try something like:

int main()
{
    string name = "here is the code" ;
    char* tmp_name = (char*) malloc ( name.size() ) ;
    strcpy ( tmp_name,name.c_str() ) ;
    for (int i = 0; i < strlen (tmp_name); ++i )
    {
        printf ( " \ncharacter %d is %c ", i + 1, tmp_name [i] ) ;
    }
    return 0 ;
}

It is pretty simple to undertand program, ofcourse there are many other ways out there to do the same thing. This was just one of them.

Thanks mate...but i need the solution in c++...is it posible??...sory for not specifying this at the start

you might look at strtok() though I don't know that the MSDN page is particularly easy to understand...

[edit:] if you need to use std::string, you could either use the .c_str() method or see if there's a split() function (I don't remember if it has one offhand).

Hmm it just requires few mods which u can esily do, but still if the below solution doesnt satisfy your requirement feel free to modify.

int main()
{
    string name = "here is the code" ;
    char* tmp_name = new char ( name.size() ) ;
    strcpy ( tmp_name,name.c_str() ) ;
    for (int i = 0; i < strlen (tmp_name); ++i )
    {
        cout << endl << "Character " << i + 1 << " is " << tmp_name [i] ;
    }
    return 0 ;
}

Thanks mate...but i need the solution in c++...is it posible??...sory for not specifying this at the start

Well this code is more C++ than C becouse in C you dont have string, only if you defined it explicitly as struct

I like to do simple C++ things ...

// split a string into its words and load them into a vector

#include <cstring>  // for strtok()?
#include <iostream>
#include <vector> 

using namespace std;

int main()
{
    char str[] = "This is a sample string.";
    char *pch;
    char delimiter[] = " ,.";  // space comma period, notice the space!!

    // create a string vector to hold the words
    vector<string> words;
    
    // get the first word/token
    pch = strtok(str, delimiter);
    while (pch != NULL)
    {
        // load the words vector
        words.push_back(pch);
        // next word
        pch = strtok(NULL, delimiter);
    }
    
    // test the words vector
    cout << "The string had " << words.size() << " words:\n";
    for (int k = 0; k < words.size(); k++)
    {
        cout << words[k] << endl;  
    }
            
    cin.get();  // wait for key press
    return EXIT_SUCCESS;  // optional
}

I love to use vectors, but you could also load an array.

Hello...I need to write a program where i need to copy each word of a string into an array...
Eg(string s="hello how are you" arr[0]=hello,arr[1]=how....).
any help:?: ??

Thanks mate...but i need the solution in c++...is it posible??...sory for not specifying this at the start

In other words, "Thanks, but I can't turn that in as homework. Please write my homework for me properly."
:confused:

commented: Exactly :) [Grunt] +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.