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

characters and word count

int main()

{

int countch=0;
int countwd=1;
cout << "Enter your sentence in lowercase: " << endl;
char ch='a';
while(ch!='\r')
{
ch=getche();
if(ch==' ')
countwd++;
else
countch++;
}
cout << "\n Words = " << countwd << endl;
cout << "Characters = " << countch-1 <<endl;
getch ();
return 0;
}

my question is when u enter "good boy" why is that the cout of words are 2 and the characters is 7?
Sep 20 '11 #1
2 2767
johny10151981
1,059 1GB
did you debug through your code??

look at the condition..
Expand|Select|Wrap|Line Numbers
  1. if(ch==' ')
  2. countwd++;
  3. else
  4. countch++;
  5.  
when ch==' ' is true the it only increase the countwd but not the countch. at the end when you press enter button it increase your countch once again. in the output you have showed countch-1. that is why you are getting 7

But if you input "I am a good boy"
The character count will be 11 but you have 16 character including '\r'

There is more: if user input "good boy ", the character count will be 7 and word count will be 3, the reason is, you are counting space inorder to count words.funny thing is the more space you add the more word count you will get
Sep 20 '11 #2
whodgson
542 512MB
In other words you are saying don't count space characters as characters.
Sep 21 '11 #3

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

Similar topics

3
by: agent mike | last post by:
I am trying to count words in a text file. I am using the following code: in_stream.get(c); if(c == ' ' || c == '.' || c == ',') word_count++; and the word count is too low. If I include "...
3
by: kaplooeymom | last post by:
I'm using Lebans RTF control in Access 2003. I'm looking for a way to get accurate word counts for a field - not including the rich text markup. I'm trying to avoid pasting the field into word,...
3
by: Oliver Saunders | last post by:
I was really shocked not to find this already submitted by someone in the PHP manual notes. <? function jnWordCount($text) { $results = array(); $pattern = '/+\S+/'; // matches wordy...
1
by: trihanhcie | last post by:
Hi I have several textarea in the same page. I would like to count the words in each textarea without any interaction with each other. Here's the code I used for the count : <script...
6
by: Jonesgj | last post by:
Hi, Whats the best way to do a word count of the content of a rich text box? Thanks in advance Jones GJ
1
by: deejayquai | last post by:
Hi Does anyone know if it is possible to display a word count as and when a user is inputting text into a memo field on a form. The code I'm using below is fine when applied to the 'On Exit'...
4
by: FunkHouse9 | last post by:
I built a form that is depentent on a word count a word count. What I have works just fine in IE but doesn't work in Firefox. It changes the count from 0 to 1 and that's as far as it goes. Here's...
0
by: =?Utf-8?B?cGFyYWhlbGlvbg==?= | last post by:
are there any programmes on vista with spell check and word count? i cant find it in word pad etc. -- parahelion
2
by: beanie | last post by:
i am a beginer in c programming and i am trying to Create a Concordance of Word Count for a Text File but my code is not working.pls can anyone helpme out.here is my code: #include <stdio.h>...
1
by: Alex Gonzalez | last post by:
Hello. I am new to C programming. I needed help with a C program that is fairly simple but I am stuck since I am new to C programming. It involves using fopen() to iterate through all files in...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.