| | |
splitting sentance into words
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
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.....
if the input is
hello world
the output i want is
hello
world
but get only hello
thanks in advance
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by blacklight332; Mar 3rd, 2009 at 10:50 pm.
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.
>#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.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
>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.
These types of codes will open your program to bufferoverflow attacks.
Use malloc to allocate memory.
for more information
http://www.cplusplus.com/reference/c...ib/malloc.html
Last edited by NicAx64; Mar 3rd, 2009 at 11:08 pm.
•
•
•
•
Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.
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.
•
•
Join Date: Mar 2008
Posts: 1,486
Reputation:
Solved Threads: 123
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
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.
But otherwise, this is an easy problem, you could even try
std::stringstream . You practically don't have to do anything C++ Syntax (Toggle Plain Text)
#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(); }
Hope this helps.
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
•
•
•
•
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.
You will end up using it in half you projects.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Using gcc to compile C and C++ files together
- Next Thread: Error in my program: always the same output ...
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






