473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Separate sentences into words with string and arrays

4
Hey all, I am resorting to posting this question after getting absolutely nowhere in four days of searching (except for maybe negative steps, got mad at my laptop and broke my delete key :( )


Anyway, my problem is that I need to separate sentences into words. I figure that the only logical way of doing this would be to use an array (and I need to for other parts of this program which I have already made)

However, I can't figure out how to gut the sentence into spaces using a loop that will pick up on the space, and put the word into one element and then start again and put the next into the next element.

So far i have tried this code, but i doubt its helpful.

Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.     for(i=0;int(word1.length());i++)
  4.   {
  5.     nextchar=word1.at(i);//get char
  6.  
  7. if(nextchar!=' ')
  8.   {
  9.     for(i=0;i<MAX;i++)
  10.           nextchar=word[i];
  11. }
  12. }
  13. }
  14.  
  15. while(nextchar!='.');
  16.  
  17.  
Anyway, I know theres probably some errors in the formatting and spelling and stuff in that, but thats my latest concoction of how to possibly do it, I know its wrong :(

Please help, I will pray for whoever helps me tonight before i go to sleep ;)

THANKS in advanced for ANY help... but if you can give me a snippet of code it would be greatly appreciated.
Oct 28 '06 #1
7 25191
xephia
4
Hey all, I am resorting to posting this question after getting absolutely nowhere in four days of searching (except for maybe negative steps, got mad at my laptop and broke my delete key :( )


Anyway, my problem is that I need to separate sentences into words. I figure that the only logical way of doing this would be to use an array (and I need to for other parts of this program which I have already made)

However, I can't figure out how to gut the sentence into spaces using a loop that will pick up on the space, and put the word into one element and then start again and put the next into the next element.

So far i have tried this code, but i doubt its helpful.

Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.     for(i=0;int(word1.length());i++)
  4.   {
  5.     nextchar=word1.at(i);//get char
  6.  
  7. if(nextchar!=' ')
  8.   {
  9.     for(i=0;i<MAX;i++)
  10.           nextchar=word[i];
  11. }
  12. }
  13. }
  14.  
  15. while(nextchar!='.');
  16.  
  17.  
Anyway, I know theres probably some errors in the formatting and spelling and stuff in that, but thats my latest concoction of how to possibly do it, I know its wrong :(

Please help, I will pray for whoever helps me tonight before i go to sleep ;)

THANKS in advanced for ANY help... but if you can give me a snippet of code it would be greatly appreciated.

P.S

this is C++
Oct 28 '06 #2
Fir3Bat
19
wats ur point xephia?
Oct 29 '06 #3
Fir3Bat
19
o nvm i didn't know you responded first lol
Oct 29 '06 #4
Ganon11
3,652 Expert 2GB
Once you have the entire string the user enters, doesn't it make sense that a word would lie between two spaces? You can consistantly search for a space in the whole sentence, and then set word equal to the proper substring of the sentence. You might need a function that will clear the spaces from in front of a word, and you will call this function before every iteration of your loop - which should be a while loop, with the condition that the sentence's length is not 0.

If you still have trouble, go ahead and ask - I recently did a similar program and will have access to the code Monday-Friday (I have it saved at school).
Oct 29 '06 #5
xephia
4
Once you have the entire string the user enters, doesn't it make sense that a word would lie between two spaces? You can consistantly search for a space in the whole sentence, and then set word equal to the proper substring of the sentence. You might need a function that will clear the spaces from in front of a word, and you will call this function before every iteration of your loop - which should be a while loop, with the condition that the sentence's length is not 0.

If you still have trouble, go ahead and ask - I recently did a similar program and will have access to the code Monday-Friday (I have it saved at school).

If you could give me that code it would be great for a starting point


I'm still stuck, this is soo frusturating
Oct 29 '06 #6
Ganon11
3,652 Expert 2GB
I'll give you the first half of my code: the main() and the function prototypes. But I'm leaving it up to you to write the function bodies. Otherwise, you wouldn't learn anything.

Expand|Select|Wrap|Line Numbers
  1. /* Chapter 8 Worksheet Program 2
  2. Independent Study, Michael Stark */
  3.  
  4. #include<iostream>
  5. #include<string>
  6. using namespace std;
  7.  
  8. void extractFirstWord(string& sentence, string& word);
  9. void processBlanks(string& sentence);
  10.  
  11. int main() {
  12.     string sentence, word;
  13.     cout << "Input a sentence: ";
  14.     getline(cin, sentence);
  15.  
  16.     while (sentence != "") {
  17.           processBlanks(sentence); // removes all blanks from the front of sentence
  18.           if (sentence.length() > 0) { // removing blanks may have made sentence null - cannot extract from a null string
  19.              extractFirstWord(sentence, word); // gets first word from sentence and puts into word
  20.              cout << word << endl; // output one word at a time
  21.           }
  22.     }
  23.  
  24.     system("PAUSE");
  25.     return 0;
  26. }
  27.  
  28. void extractFirstWord(string& sentence, string& word);
  29. // extractFirstWord removes the substring of sentence 
  30. // from the beginning to the first space from sentence 
  31. // and stores the same string into word. sentence is
  32. // shortened accordingly.
  33. // Postcondition: sentence is shortened, and word
  34. //                is appropriately the first word in
  35. //                sentence.
  36.  
  37. void processBlanks(string& sentence);
  38. // processBlanks will remove all of the spaces in front
  39. // of sentence.
  40. // Postcondition: sentence has no spaces in front of
  41. //                the first word.
Oct 30 '06 #7
xephia
4
Thanksss!! that was alot of help!
Nov 1 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
6
by: ASPfool | last post by:
Hello everyone, Please help me before I throw my computer out of the window: I'm working on a web application in classic ASP (vbscript), which is making calls to some webservices. The calls...
2
by: zlf | last post by:
Hi, This there any existing alorgithm in .NET that can extract the common element in two string arrays to another string array; usage: string a = { "1","2","3" }; string b = { "1" }; string...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
0
by: Caffiend | last post by:
Hello again! I am still having problems with the code under the title "newbie woes", but I have moved on for now to another exercise. The book has asked me to write a program that will use two...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
8
by: rsaikamesh | last post by:
Hi All, In my c++ application, I need to use 60 string arrays with the size of 16. If I create this much of arrays, will the performance of my application be affected?
0
by: =?Utf-8?B?YWlhaG1lZA==?= | last post by:
I am trying to call a Webservice written in Java from .NET C# client. I am getting the following error : Exception Details: System.Web.Services.Protocols.SoapException: Server was unable to...
6
by: tshad | last post by:
I am looking for a way to combine 2 string arrays. I am trying to get a list of files from 2 directories and combine them: string strFiles; string strFiles2; strFiles =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.