| | |
letter and word counter
![]() |
•
•
Join Date: Dec 2004
Posts: 1
Reputation:
Solved Threads: 0
i need to write a program that can count the number words the user enters and and the recurences of each letter.
ex: Have a nice day
Total words: 4
a = 3
d = 1
e = 2
...
Can't get the program i've writen so far to count the number of words.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int freq[sizeof letters - 1] = {0};
char text[60];
char input;
int numWords=0;
bool inword = false;
cout<<"Enter a line of text: \n";
cin>>input;
while((input=cin.get()) !='\n')
{ // Counts the number of words in the text.
if(input == ' ' || input== '\t')
{
inword = false;
}
else if (inword == false)
{
inword = true;
numWords++;
}
cout<<"The number of words in the text is: "<<numWords<<endl;
if ( cin.getline ( text, sizeof text ) )
{
for ( int i = 0; text[i] != '\0'; i++ )
{// Converts all letters to lower case.
if ( isalpha ( text[i] ) )
++freq[tolower ( text[i] ) - 'a'];
}
for ( int i = 0; letters[i] != '\0'; i++ )
{// Counts the frequency of all letters.
if ( freq[i] != 0 )
cout<< letters[i] <<": "<< freq[i] <<endl;
}
}
}
return 0;
}
thanks...
ex: Have a nice day
Total words: 4
a = 3
d = 1
e = 2
...
Can't get the program i've writen so far to count the number of words.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int freq[sizeof letters - 1] = {0};
char text[60];
char input;
int numWords=0;
bool inword = false;
cout<<"Enter a line of text: \n";
cin>>input;
while((input=cin.get()) !='\n')
{ // Counts the number of words in the text.
if(input == ' ' || input== '\t')
{
inword = false;
}
else if (inword == false)
{
inword = true;
numWords++;
}
cout<<"The number of words in the text is: "<<numWords<<endl;
if ( cin.getline ( text, sizeof text ) )
{
for ( int i = 0; text[i] != '\0'; i++ )
{// Converts all letters to lower case.
if ( isalpha ( text[i] ) )
++freq[tolower ( text[i] ) - 'a'];
}
for ( int i = 0; letters[i] != '\0'; i++ )
{// Counts the frequency of all letters.
if ( freq[i] != 0 )
cout<< letters[i] <<": "<< freq[i] <<endl;
}
}
}
return 0;
}
thanks...
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
sizeof letters: needs brackets around letters, but that could be the absence of code tags (use [xCODE]paste code here[x/CODE] --> remove x's wen u post: i put that in so u could see the tags!!)
when initialiseing the frequency table you would need to set every element to zero to be sure, you have only set the first. using a loop is a much better idea.
have you tried cout'ing input back to the console to check the RIGHT chars are going in?? i have been through the loop a few times in my head and have tested it like so
This counter works, so your code should... using this code will get you the number of words, see if you can get it working and post back with any results. hope this helps
when initialiseing the frequency table you would need to set every element to zero to be sure, you have only set the first. using a loop is a much better idea.
have you tried cout'ing input back to the console to check the RIGHT chars are going in?? i have been through the loop a few times in my head and have tested it like so
C Syntax (Toggle Plain Text)
#include <iostream.h> int main(void) { bool inword = 0; unsigned int numwords = 0; char *str = "this is a test string \t \n"; // 5 words.... char letter; // comparison letter while (letter = *str++) { if(letter == ' ' || letter == '\t' || letter == '\n') { inword = 0; } else if(inword == 0) { inword = 1; numwords++; } } cout << "Found " << numwords << " words!\n"; // it works!! return 0; }
This counter works, so your code should... using this code will get you the number of words, see if you can get it working and post back with any results. hope this helps
Last edited by 1o0oBhP; Dec 15th, 2004 at 1:27 pm. Reason: Making Code tag visible
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
•
•
Originally Posted by 1o0oBhP
sizeof letters: needs brackets around letters
•
•
•
•
Originally Posted by 1o0oBhP
when initialiseing the frequency table you would need to set every element to zero to be sure, you have only set the first. using a loop is a much better idea.
•
•
•
•
The remainder of the array is also initialized to zero. Using a loop is not a better idea
•
•
•
•
You need parentheses for a type, but not an object.
#BTW: I tried your code chriswell, the cin.get() line seems to miss the first word (if you cout the input). I thought if you cout input you should get the same line you entered in, but when i tried it failed. It seems this is the critical flaw.
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
•
•
Originally Posted by chriswell
i need to write a program that can count the number words the user enters and and the recurences of each letter.
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int freq[sizeof letters - 1] = {0};
char text[60];
int numWords = 0;
bool inword = false;
cout<<"Enter a line of text: \n";
if ( cin.getline ( text, sizeof text ) )
{
for ( int i = 0; text[i] != '\0'; i++ )
{// Converts all letters to lower case.
if ( isalpha ( text[i] ) )
{
++freq[tolower ( text[i] ) - 'a'];
}
// Counts the number of words in the text.
if ( isspace(text[i]) )
{
inword = false;
}
else if ( inword == false )
{
inword = true;
numWords++;
}
}
for ( int i = 0; letters[i] != '\0'; i++ )
{// Counts the frequency of all letters.
if ( freq[i] != 0 )
cout<< letters[i] <<": "<< freq[i] <<endl;
}
}
cout<<"The number of words in the text is: "<<numWords<<endl;
return 0;
} something like this
Most of the time its ok and the rest is blank. Occasionally its not and causes errors when you use strcat and other string functions. I recently had the same problem on a small log project using mainly fstream and strcat. Playing it safe. I realise numericals are initialised zero but i have seen in the past cout giving scientific numbers. Ive dealt with enough random errors that i always make sure everything is concrete.
C Syntax (Toggle Plain Text)
char text[256] = "1o0oBhP";
Most of the time its ok and the rest is blank. Occasionally its not and causes errors when you use strcat and other string functions. I recently had the same problem on a small log project using mainly fstream and strcat. Playing it safe. I realise numericals are initialised zero but i have seen in the past cout giving scientific numbers. Ive dealt with enough random errors that i always make sure everything is concrete.
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
•
•
Originally Posted by 1o0oBhP
Most of the time its ok and the rest is blank. Occasionally its not and causes errors when you use strcat and other string functions.
•
•
•
•
Originally Posted by 1o0oBhP
Playing it safe. I realise numericals are initialised zero but i have seen in the past cout giving scientific numbers. Ive dealt with enough random errors that i always make sure everything is concrete.
fair enough. Im used to Programming in VB (6 years and before i learned c++) where you can observe variables at run time (ie wats in the string). however my c++ IDE doesnt.... so i dont know what is going on i have to make certain nothing can go wrong 
Do you use Dev-C++? I have 4.9.9.0 and if anyone knows how can i observe vaiables at run time??

Do you use Dev-C++? I have 4.9.9.0 and if anyone knows how can i observe vaiables at run time??
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- Capitalizing first letter of word in an array? (C)
- word counter, frequency, percentage (Java)
- word counter (C++)
Other Threads in the C Forum
- Previous Thread: SDK FOR Windows XP
- Next Thread: Gui autogenerated code in .h instead of .cpp
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






