473,320 Members | 2,104 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,320 software developers and data experts.

Splitting string into words and displaying

12
I need to Write a function that will, given an input string containing many words, split that string into individual words. For each word, the function should output the word, its starting index in the string, and its length to the console without using stream extraction operator.

#include <iostream>
#include <string>
using namespace std;
int main()
{

string sval = " The quick brown fox jumps over the lazy dog";

int cnt=1;
int nsep = sval.find(" ");
while (nsep > 0) {
cout << "Word " << cnt << "="
<< sval.substr(0,nsep) << endl;
cnt = cnt + 1;
sval = sval.substr(nsep+1);
nsep = sval.find(" ");

}
cout << "Word " << cnt << "=" << sval << endl;

#ifdef WIN32
system("pause");
#endif
return(0);
}


Thats where i am so far, it will just split it into words but will not work if it begins with a space or if there is more than 1 space between words.

any help?
Nov 2 '06 #1
3 5217
Ramper
12
I think i need to use the s.find_first_not_of but not sure how to incorporate.
Nov 2 '06 #2
Ganon11
3,652 Expert 2GB
Make a function that takes a string variable by reference as its argument, and have it remove all blanks from the start of the string. This will eliminate the problem of a string starting with spaces and multiple spaces seperating words. Call this function before you extract a word.
Nov 2 '06 #3
Hi,

Try this code...
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. char str[] = "The quick brown fox jumps over the lazy dog" ;
  6. int i,prev,w_len;
  7. prev = w_len = 0;
  8. for(i=0;i<sizeof(str);i++)
  9. {
  10. if(str[i] == ' ' || str[i] == '\0' )
  11. {
  12. w_len = (i - prev);
  13. printf("\t%d\t%d\t\n", i,w_len);
  14. prev = i +1 ;
  15. }
  16. else
  17. printf("%c",str[i]);
  18. }
  19.  
  20. return 0;
  21. }
  22.  
~David
Nov 3 '06 #4

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

Similar topics

2
by: Piotr | last post by:
Is there any way to split all merged words but www and e-mail addresses? I have regexp preg_replace("/(\.)(])/", "\\1 \\2", "www.google.com any,merged.words mymail@domain.com") it give me...
6
by: qwweeeit | last post by:
Splitting with RE has (for me!) misterious behaviour! I want to get the words from this string: s= 'This+(that)= a.string!!!' in a list like that considering "a.string" as a word. Python...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
7
by: Anat | last post by:
Hi, What regex do I need to split a string, using javascript's split method, into words-array? Splitting accroding to whitespaces only is not enough, I need to split according to whitespace,...
2
by: Anat | last post by:
Hi, I need a little help on performing string manipulation: I want to take a given string, and make certain words hyperlinks. For example: "Hello world, this is a wonderful day!" I'd like the...
12
by: Simon | last post by:
Well, the title's pretty descriptive; how would I be able to take a line of input like this: getline(cin,mostrecentline); And split into an (flexible) array of strings. For example: "do this...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: techusky | last post by:
I am making a website for a newspaper, and I am having difficulty figuring out how to take a string (the body of an article) and break it up into three new strings so that I can display them in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.