Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,376 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,621 people online right now. Registration is fast and FREE... Join Now!




how to seperate punctuation mark from word.

 
Reply to this topicStart new topic

how to seperate punctuation mark from word.

dreamz
22 Jan, 2008 - 07:28 PM
Post #1

New D.I.C Head
*

Joined: 22 Jan, 2008
Posts: 2

Hi,
I have to write a program which counts the number of words in a string. but a word like yes!...I need to seperate the yes and !. I have no idea how to do this. This is the code i have written so far
CODE

char * cstr, *p;

  string str ("yes i can! yes yes no no ! s");

  cstr = new char [str.size()+1];
  strcpy (cstr, str.c_str());


  p=strtok (cstr," ");
  while (p!=NULL)
  {
    cout << p << endl;;
    c.add(p);   // counts the number of words by using pointers
    p=strtok(NULL," ");
  //    c.add(p);
  }

  delete[] cstr;

User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: How To Seperate Punctuation Mark From Word.
22 Jan, 2008 - 07:50 PM
Post #2

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 994



Thanked: 4 times
Dream Kudos: 375
My Contributions
Post your code like this:
code.gif

When you count how many words in a file or in a string, first you need to know what is a "word"? how is it different from a single character such as ! or letter.

The technique of counting a word is to count the space which separate from the other words.


User is offlineProfile CardPM
+Quote Post

dreamz
RE: How To Seperate Punctuation Mark From Word.
22 Jan, 2008 - 08:17 PM
Post #3

New D.I.C Head
*

Joined: 22 Jan, 2008
Posts: 2

QUOTE(nirvanarupali @ 22 Jan, 2008 - 08:50 PM) *

Post your code like this:
:code:

When you count how many words in a file or in a string, first you need to know what is a "word"? how is it different from a single character such as ! or letter.

The technique of counting a word is to count the space which separate from the other words.


I understand that words are seperated by space ina string but what if the string is "yes! I did it." There is no space b/t yes and ! so yes! is displayed as one word I have to copune the words and punctuation marks as well. How do i seperate teh word from the punctuationmark.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month