hey guys i want to slpit a sentence into words but the pogram i have written gives only the first word...can u help me plz.....

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<stdio.h>

void main()
{
clrscr();
char text[100];
int i=0;

clrscr();
cout<<"enter the sentence";
cin.getline(text,100);

while(i<strlen(text))
{
char text1[]=" ";
int m=0;
while(text[i]!=' ')
{
text1[m]=text[i];
m++ ;
i++;
}
i++;
cout<<"\n"<<text1;
}

getch();
}

if the input is

hello world

the output i want is

hello
world

but get only hello

thanks in advance

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> explode( const string &delimiter, const string &explodeme);

int main(int argc, char *argv[])
{
    string str = "I have a lovely bunch of cocoa nuts";
    cout<<str<<endl;
    vector<string> v = explode(" ", str);
    for(int i=0; i<v.size(); i++)
        cout <<i << " ["<< v[i] <<"] " <<endl;
}

vector<string> explode( const string &delimiter, const string &str)
{
    vector<string> arr;

    int strleng = str.length();
    int delleng = delimiter.length();
    if (delleng==0)
        return arr;//no change

    int i=0;
    int k=0;
    while( i<strleng )
    {
        int j=0;
        while (i+j<strleng && j<delleng && str[i+j]==delimiter[j])
            j++;
        if (j==delleng)//found delimiter
        {
            arr.push_back(  str.substr(k, i-k) );
            i+=delleng;
            k=i;
        }
        else
        {
            i++;
        }
    }
    arr.push_back(  str.substr(k, i-k) );
    return arr;
}

source: cpp explode function

You've got a lot of problems with your code, but you do get bonus marks for being one of the few people who use code tags on their first post!

>#include<iostream.h>
>#include<string.h>
Those headers are nonstandard and not recommended. Try removing the ".h" from them and adding the line "using namespace std;" below them.

>#include<conio.h>
>#include<dos.h>
Old headers. Get rid of them.

>#include<stdio.h>
This is a C header. You shouldn't need or use it either.

>void main()
void main() is also nonstandard and bad. Use int main() instead.

>clrscr();
Don't clear the screen. It relies on those old headers, and annoys people like me who might actually have important data on the screen before you wiped it.

>char text1[]=" ";
You're only allocating 2 bytes (one for the space, one for the terminating null character) to hold an entire word. Nope, that's not a good idea.

Instead of using character arrays, consider using a C++ string. You don't need to worry about character allocation, and it makes your job a heck of a lot easier. If you're confident enough, you could even use string's built in searching functions to do the tokenizing instead of implementing your own.

>getch();
This relies on the nonstandard conio.h header. Try using getchar(); instead.

go carefully again and again with your algorithm .

If you unable to find it then just try a dry-run.
Entering it the input "hello world"

Think "hello world " and the "hello world" is different
I think you got the point.

>char text1[]=" ";
You're only allocating 2 bytes (one for the space, one for the terminating null character) to hold an entire word. Nope, that's not a good idea.

I wonder how this program works. However this will vary due to the memory model that he/she is using.

These types of codes will open your program to bufferoverflow attacks.

Use malloc to allocate memory.
for more information
http://www.cplusplus.com/reference/clibrary/cstdlib/malloc.html

Member Avatar for jencas

Use malloc to allocate memory.

No, no, no! Use new to allocate memory in C++!!!

Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.

Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.

agree to this !
use the string wrapper class is agreed ! Using the STL as max is somewhat encouraged.

However there are algorithms and every data structures inside the STL . But don't we write our own Stacks and link lists in our school ?

you don't need this while loop at all if you are using the std::string. It comming with powerful functions that already implemented what you are trying to hand-code.

If you have to implement your own algorithm to do this, try taking a look at a snippet I made some time ago. Link

But otherwise, this is an easy problem, you could even try std::stringstream . You practically don't have to do anything :icon_lol:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
  string sentence_s;
  stringstream sentence_ss;

  cout << "Enter a sentence: ";
  getline(cin, sentence_s);
  sentence_ss << sentence_s;

  string word;

  while ( sentence_ss >> word )
    cout << '\n' << word;

  cin.ignore();
}

If you can't do something like this, try using strtok , it will help you split up the word using a delimiter.

Hope this helps.

If you can't do something like this, try using strtok, it will help you split up the word using a delimiter.

Hope this helps.

Yes a tokenizer is highly recommended.
You will end up using it in half you projects.

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.