473,387 Members | 1,573 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,387 software developers and data experts.

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

17
//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

✓ answered by whodgson

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.

4 2659
whodgson
542 512MB
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
Thank you whodgson, yes I forgotten to put some braces. I got it now.
Nov 27 '09 #3
donbock
2,426 Expert 2GB
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
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
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") ||...
12
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...
8
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...
23
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...
1
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...
6
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...
1
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...
0
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, ...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...

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.