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

need ur urgent help

how to find the number of words present in a string which may contain more than 1 blanks between two words and may have leading or tailing blanks.

example:--- my name is abhishek sharma .
Oct 26 '06 #1
2 1125
dtimes6
73
You can use something called flex.
Oct 26 '06 #2
Ganon11
3,652 Expert 2GB
Consider the following code:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. void extractFirstWord(string& sentence, string& word);
  6. void processBlanks(string& sentence);
  7.  
  8. int main() {
  9.     string sentence, word; //definition of variables
  10.     cout << "Input a sentence: ";
  11.     getline(cin, sentence); //sentence is initialized to the user input
  12.  
  13.     while (sentence != "") { // while sentence still contains characters
  14.           processBlanks(sentence); // removes blanks in front of the next word
  15.           if (sentence.length() > 0) { //removing blanks may set sentence to "" - must check if sentence has a word to be extracted
  16.              extractFirstWord(sentence, word); // word is set to the first word in sentence - sentence is shortened, beginning just after word
  17.              cout << word << endl; // prints the word - here, you can replace the cout with whatever processes you need, such as a sum counter
  18.           }
  19.     }
  20.  
  21.     system("PAUSE");
  22.     return 0;
  23. }
  24.  
  25. void extractFirstWord(string& sentence, string& word) {
  26.      int pos = sentence.find(' '); // find the next occurance of a space
  27.      if (pos != string::npos) { // if the string has spaces remaining
  28.         word = sentence.substr(0, pos); // word is set to the string from position 0 in sentence to the position of the space
  29.         sentence = sentence.substr(pos); // sentence is shortened - it now equals everything after word in the original sentence
  30.      } else { // if the string has no spaces - a.k.a., the only thing left in sentence is the last word
  31.         word = sentence; // word is set to sentence
  32.         sentence = ""; // sentence is cleared
  33.      }
  34.      return;
  35. }
  36.  
  37. void processBlanks(string& sentence) {
  38.  
  39.      while (sentence.length() > 0 && sentence.substr(0, 1) == " ") // Removes blanks - while the first character is a blank, remove that character
  40.            sentence = sentence.substr(1);
  41.      return;
  42. }
  43.  
  44. /* Sample Run
  45.  
  46. Input a sentence:    Only  when     I used     weird things  such as  messed  up
  47.  spaces  did     this work!
  48. Only
  49. when
  50. I
  51. used
  52. weird
  53. things
  54. such
  55. as
  56. messed
  57. up
  58. spaces
  59. did
  60. this
  61. work!
  62. Press any key to continue . . .
  63.  
  64. Input a sentence:      Does   fuzzy logic   tickle?    When you  choke a smurf,
  65. what   color  does it turn?
  66. Does
  67. fuzzy
  68. logic
  69. tickle?
  70. When
  71. you
  72. choke
  73. a
  74. smurf,
  75. what
  76. color
  77. does
  78. it
  79. turn?
  80. Press any key to continue . . .
  81.  
  82. */
Will this work?

In Java, there was a class called stringTokenizer that would take a string in its constructor and seperate it by a character you specified (default was space). This class would solve your problem - unfortunately, no such class exists in the C++ standard library. Thus, I created one! Please PM me if you'd like a copy of the class.
Oct 26 '06 #3

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

Similar topics

1
by: Jenny | last post by:
Need urgent help for an unsolved problem. In our ASP web application, we creat a Back button and if user click on this button, it execute history.go(-1) to go back to the previous page. All our...
3
by: Rahul S. | last post by:
Hey all: I need urgent information how to set the environment in Visual.NET wiritng in C. I am working with a licensed version of the Analyze 6.0 software from mayo clinic. I need to write...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
2
by: Max Power | last post by:
Hi All I am coding a small app in that swaps specific files between a client and server. All files and locations are set at both sides. I want my app to show a file list, based on the file...
1
by: TexCube | last post by:
Hello everyone, I have an urgent question. Can data be pulled from an access database table to create a directory that can be printed out and bound together? I need to have each row of a...
10
by: David Thielen | last post by:
Hi; I have help html pages for each page of my ASP.NET webapp. So for the page datasource.aspx, I have help\datasource.htm. Bu what I want when the hyperlink is clicked, for it to look for the...
0
by: sanjayhire | last post by:
Hi All, I have just joined the forum. I need your help..As it's very urgent... How to identify a particular process is running on a server(Windows/Unix server) using VB program and since how...
1
by: psantosh12 | last post by:
Hello Frnds Please need help to resolve error.......... it is very very urgent........ The error is Runtime Error Description: An application error occurred on the server. The current custom...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.