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. -
do
-
{
-
for(i=0;int(word1.length());i++)
-
{
-
nextchar=word1.at(i);//get char
-
-
if(nextchar!=' ')
-
{
-
for(i=0;i<MAX;i++)
-
nextchar=word[i];
-
}
-
}
-
}
-
-
while(nextchar!='.');
-
-
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.
7 25191
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. -
do
-
{
-
for(i=0;int(word1.length());i++)
-
{
-
nextchar=word1.at(i);//get char
-
-
if(nextchar!=' ')
-
{
-
for(i=0;i<MAX;i++)
-
nextchar=word[i];
-
}
-
}
-
}
-
-
while(nextchar!='.');
-
-
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++
o nvm i didn't know you responded first lol
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).
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
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. - /* Chapter 8 Worksheet Program 2
-
Independent Study, Michael Stark */
-
-
#include<iostream>
-
#include<string>
-
using namespace std;
-
-
void extractFirstWord(string& sentence, string& word);
-
void processBlanks(string& sentence);
-
-
int main() {
-
string sentence, word;
-
cout << "Input a sentence: ";
-
getline(cin, sentence);
-
-
while (sentence != "") {
-
processBlanks(sentence); // removes all blanks from the front of sentence
-
if (sentence.length() > 0) { // removing blanks may have made sentence null - cannot extract from a null string
-
extractFirstWord(sentence, word); // gets first word from sentence and puts into word
-
cout << word << endl; // output one word at a time
-
}
-
}
-
-
system("PAUSE");
-
return 0;
-
}
-
-
void extractFirstWord(string& sentence, string& word);
-
// extractFirstWord removes the substring of sentence
-
// from the beginning to the first space from sentence
-
// and stores the same string into word. sentence is
-
// shortened accordingly.
-
// Postcondition: sentence is shortened, and word
-
// is appropriately the first word in
-
// sentence.
-
-
void processBlanks(string& sentence);
-
// processBlanks will remove all of the spaces in front
-
// of sentence.
-
// Postcondition: sentence has no spaces in front of
-
// the first word.
Thanksss!! that was alot of help!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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?
|
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...
|
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 =...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |