473,765 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I have this function that should count how many characters are letters in the English

17 New Member
//It should count how many characters are letters in the English alphabet, and is
displaying the correct letters, but is not counting good.

Expand|Select|Wrap|Line Numbers
  1. void FunctionCountLetters(char s[])
  2. {
  3. int len;
  4. int i;
  5. len=strlen(s);
  6. cout<<"\n The lenght of the string is= "<<len<<endl;
  7.  
  8. int counter=0;
  9. for(i=0; i<len; i++)
  10. {
  11. if((s[i]>='a'&& s[i]<='z')||(s[i]>='A'&& s[i]<='Z'))
  12.    cout<<"The letters in the English alphabet are: "<<s[i]<<endl;
  13.    counter++;
  14.  
  15. }//end of for loop.
  16.  
  17. cout<<"The number of letters in the English alphabet are: "<<counter;
  18.  
  19.  
  20. }//end of function count letters.
Nov 27 '09 #1
4 2688
whodgson
542 Contributor
I think you need to put some braces round the body of the if statement.
Also its much easier for people if you enclose your code in [CODE]...[/code]
tags. Also if you change the string to all capitals or all lower case you can halve the length of your if statement.
Nov 27 '09 #2
abueno
17 New Member
Thank you whodgson, yes I forgotten to put some braces. I got it now.
Nov 27 '09 #3
donbock
2,426 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. if((s[i]>='a'&& s[i]<='z')||(s[i]>='A'&& s[i]<='Z')) 
Notice that this code assumes that the character code for 'a' is less than 'z', and that the character codes for all the lowercase letters fall between them, and that there are no other character codes in that range; and that the same holds true for 'A' and 'Z'.

This is true for ASCII, but not for other character codes your program might encounter. For example, EBCDIC does not conform to this assumption.

You want to do the following in order to be fully portable:
Expand|Select|Wrap|Line Numbers
  1. if(isalpha(s[i]))
Dec 1 '09 #4
abueno
17 New Member
Thank you donbock, that's true....
Dec 1 '09 #5

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

Similar topics

12
1724
by: Robert | last post by:
Hi, I was wanting to write some code to work out whether I needed st, nd rd or th in my dates. While the code below works, I am certain that there is a better way.... if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){ $letters = "st"; } elseif (($daterep == "2") || ($daterep == "22")){ $letters = "nd";
12
8228
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I not - be able to use non-ASCII characters in strings and in Tk GUI button labels and GUI window titles and in raw_input data without Python returning wrong case in manipulated
8
6448
by: Beznas | last post by:
Hi All; I'm trying to create an ASP function called CleanX that removes the punctuation and some characters like (*&^%$#@!<>?"}|{..) from a text string I came up with this but It doesn't look like it's working. Can anyone help please. THANK YOU.
23
7175
by: Stan Goodman | last post by:
I run a website for my very extended family. The site is not a static one, and pages are frequently added and changed. I constructed it by myself, but I can best be described as a casual and unsophisticated web designer. Because I have always had difficulty in producing dashes on my pages, I generally use double hyphens instead. Books that I possess or have seen on HTML tell me that I could make e.g. an em-dash by using the escape...
1
1927
by: carl bloc | last post by:
Have this much done but need help with this bit of code: allow user to modify existing draw data. I think I need a counter to give week numbers so the user can select a week number rather than a date. I need to view data for a selected week given the week number or date. need some controls on not allowing the ball number to go >49 or <0 and no duplicates.
6
4900
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
1
1634
by: vv1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, print the decoded message. Use the following key for decoding.Convert any numeric characters in input (0-9) to a space in the output. Upper case letters in input/message string are decoded by adding 19 to their ASCII/Integer value. Lower...
0
1418
by: vp1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, print the decoded message. Use the following key for decoding.Convert any numeric characters in input (0-9) to a space in the output. Upper case letters in input/message string are decoded by adding 19 to their ASCII/Integer value. ...
3
1974
NewYorker
by: NewYorker | last post by:
Hello brothers and sisters, Please help me complete this program and get the output shown below. Here is all I have ___________________________________________________________ This homework involves reading sentences from a file and determining the makeup of the sentences, words and characters. 1. First, you will read in the sentences with the function void readSentences(string sentences, int &numSentences)
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.