Click here to Skip to main content
15,891,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// helpword.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



int main () 
{
	
	int space = 0 ;
  string myString = "" ; 
  int i = 0 , p ;
	cout << "enter a sentence :"  << endl ;
	getline( cin , myString ) ;	
	cout << myString.length() << endl ;
	
	   

	//for(int i = 0; i < myString.length(); i++){
	//	if(myString.at(i) ==  ' ' )
	//	{
	//		h++;
	//	}
	//}cout<<"words"<<h<<endl;
	//  while((myString)  != '*'  )
 // {
 //   character ++;
 //   if(ch == ' ')
	//word ++;
 // }

 //
 // cout<< "the characters are :" << character + 1  << endl ;
 // cout<< "the words are :" << word+1 << endl ;

	while( i < myString.length() )
	{
	p = i ;
	while( p < 10 + i )
	{
		cout << myString.at(p) ;
		p++ ;
	}
		i += 10 ;
		cout << "\n" ;
	}
	while(myString == ' ')
	{
		space++;
	}
	cout <<"word"<<space + 1 <<endl ;
	
    return 0;
}


</string></iostream>

I cannot work with string
Posted
Updated 4-Apr-11 10:40am
v3
Comments
ZeeroC00l 4-Apr-11 13:12pm    
What are you trying to do with this code ? Can you be more specific with the question ?
yay hello 4-Apr-11 13:49pm    
i want the numbers of words in a sentence
Аslam Iqbal 4-Apr-11 14:13pm    
Are trying to find, how many words in myString variable? Or how many words of your source code file?
ZeeroC00l 4-Apr-11 14:23pm    
Ok, if so then why would you increase your counter by 10 every time ?
Dalek Dave 4-Apr-11 16:40pm    
Edited for Grammar.

C#
int get_word_count(char *stringInput)
{
  int count = 0;
  char *s;

  while(IS_PUNC(*stringInput))
    stringInput++;
  s = stringInput;

  while(*stringInput)
  {
    while(*s && !IS_PUNC(*s))
      s++;
    if(stringInput - s)
      count++;
    while(IS_PUNC(*s))
      s++;
    stringInput = s;
  }

  return count;
}


This will get you the count of the words in a sentence.

Or otherwise there is another easy way to do it. That is to use the strtok functionality provided by the C++ string.h header.

char * strtok ( char * str, const char * delimiters );


Above is the syntax of how to use the strtok functionality.

strtok[^]

Take a look at the code example. YOu must be able to solve your problem after looking into that
 
Share this answer
 
v2
Comments
Dalek Dave 4-Apr-11 16:41pm    
Good Answer.
Using The Standard C++ Library can significantly simplify your task.
I will assume that the words are white space separated, here is a demo application with
two different implementations of WordCount, both using the standard library utilities:
C++
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>

// First alternative
int WordCount1(const std::string& str)
{
    int count = 0;
    std::istringstream iss(str);
    for(std::string word; iss >> word; ++count);
    return count;
}

// Second alternative
int WordCount2(const std::string& str)
{
    std::istringstream iss(str);
    return std::distance(std::istream_iterator<std::string>(iss),
                         std::istream_iterator<std::string>());
}

// Lets make a test
int main()
{
    // The test string - note that it contains '\t' and '\n'
    std::string str = "This\tis a test!\nI repeat, this is a test!";
    
    // Show the results:
    std::cout << "\nWordCount1: " << WordCount1(str) << " words"
              << "\nWordCount2: " << WordCount2(str) << " words";
    
    return 0;
}


The program output is:
WordCount1: 10 words
WordCount2: 10 words


You can see the simplicity here... Especially in second alternative which is just 2 lines of code.
Also note that tab ('\t') and new line ('\n') characters are also correctly interpreted.

If you want to understand how these algorithms work check out the documentation for std::istream::operator>>[^] and std::distance[^].

If the documentation is not clear enough for you, you are always welcome to share your concerns here. :)

Best Regards,
Nuri
 
Share this answer
 
If you use one of the standard typing definitions where a word is 5 characters then you just need to count the number of characters then divide by 5.
For example you could write:

FileInfo yourFile = new FileInfo(pathName);
int numberOfWords = yourFile.Length/5;


Where pathName is the path to and name of your file. This will allow you to find the number of words in your program as you asked.
 
Share this answer
 
v3
Comments
yay hello 4-Apr-11 13:50pm    
how ?
BillW33 4-Apr-11 14:27pm    
I modified my answer to respond to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900