954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Word count

im having a problem...i have a program to count number of senteces one inputs..the words and asking for a letter. The end of "\\close count" works fine...but for some reason the sentence and word programs doesnt work. could anyone please check this out. I think Iam missing something. I have 3 three separate but I need them to link together into one for it to work. Thanks for the help

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

using namespace std;

void main()
{
		int count(char *, char), words, senten;
       char userString[51], letter;
		 cout << "Enter a sentence : ";
		 cin.getline(userString, 51);
		 cout << "Enter a letter \n";
       cin >> letter;
       cout << letter << " appears ";
       cout << count(userString, letter) << " times.\n";
} //close main
int count(char *str, char ch)
{
       int times = 0;
       while(*str != '\0')
		 {
		 if (*str == ch)
		 times++;
		 str++;
		 }//close while
       return times;
}// close count

int words
{
		int words = 1;
		while(*str != '\0')
		{
		string userString;
		getline(cin, userString);
		for(i = 0; i < userString.length(); i++)
		{
	  if(userString.at(i) == ' ')
			words++;
		}
	  cout << "Number of words in text: " << words << endl;
        }//close words
}

int senten
{
	int senten = 0;
	while (*str == ch)
    {
        switch (ch) 
		{
                case '.' || case '?' :
                sentence_count++;
                break;
               
    cout << "There is/are " << senten << " sentence/s" << endl;

    return 0;


<< moderator edit: added [code][/code] tags >>

NejiHyuuga
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Umm you haven't called them in main? Or was there more to this?

oboler
Newbie Poster
13 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

no more to this.. I wanted the program to count...but i put Int words, sentence and and i assigned a character.

NejiHyuuga
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Reread your text on basic syntax.

int words
{
		int words = 1;


It looks like VB. Functions should have a parenthesized parameter list, and it's not a good idea to name a local variable the same as the function.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I changed the words around..again using

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

void main()
{
		int count(char *, char), wor, senten;
       char userString[51], letter;
		 cout << "Enter a sentence : ";
		 cin.getline(userString, 51);
		 cout << "Enter a letter \n";
       cin >> letter;
       cout << letter << " appears ";
		 cout << count(userString, letter) << " times.\n";
		 cout << "Number of words in text: " << words << endl;
		 cout << "There is/are " << senten << " sentence/s" << endl;
      }//end main
int count(char *str, char ch)
{
       int times = 0;
       while(*str != '\0')
		 {
		 if (*str == ch)
		 times++;
		 str++;
		 }//close while
       return times;
}// close count

int wor;
{
		int words = 1;
		while(*str != '\0')
		{
		string userString;
		getline(cin, userString);
		for(i = 0; i < userString.length(); i++)
		{
	  if(userString.at(i) == ' ')
			words++;
		}
	  cout << "Number of words in text: " << words << endl;
		  }//close words
}

int senten
{
	int sentence_count = 0;
	while (*str == ch)
    {
        switch (ch) 
		{
                case '.' || case '?' :
                sentence_count++;
                break;
	 }
	 cout << "There is/are " << senten << " sentence/s" << endl;
	  }
	 return 0;
}


<< moderator edit: added [code][/code] tags >>

example of what should happen:
Enter a sentence: programming is very hard. [enter]
There are 4 words
There is/are 1 sentence/s
Enter a letter: m [enter]
m is used 2 times

I need to know what is going on with this program.

NejiHyuuga
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
I need to know what is going on with this program.

It fails to compile because of basic syntax errors.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Oh i see thanx

NejiHyuuga
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[60];
int i,j=0;
clrscr();
for(i=0;i<=60;i++)
a[i]='\0';
printf("\nEnter the string:");
scanf("%[^\n]",a);

for(i=0;i<=strlen(a);i++)
{
if(a[i]==' '&& a[i+1]!=' '&&a[i]!='\t')
{
if(i!=0)
j++;
}
else
{
if(a[i]=='\t'&&a[i+1]!=' '&&a[i+1]!='\t'){
j++;
}}
}
j++;
printf("\nThe number of the words:%d",j);
getch();
}
chellag
Newbie Poster
1 post since Mar 2010
Reputation Points: 19
Solved Threads: 0
 

Code by chellag is failing if i Give input as :
This is a string

where the space between any 2 words is 4-5 spaces

manali04
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Consider this:-

#include<iostream> 
using namespace std; 
int main() 
{ 
char str[80];
int len, count=0;
cout << "Enter a string (Max 80 characters):";
gets(str);
len=strlen(str);
for (int i=0; i<len; i++)
	{ if ((i!=len-1 && str[i] == ' ' && str[i+1] != ' ') )
	  count =count+1;
		}
cout << "Number of words in the string are:"<<count <<endl;
getchar();
return 0;

}
manali04
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You