Hi everyone,
just want to share simple program that will count the number of characters, integers, space and punctuations in a string..
just want to share simple program that will count the number of characters, integers, space and punctuations in a string..
#include <iostream> #include <string> #include <cctype> using namespace std; void showValues(string, int); int main() { string sentence; cout << "Enter sentence now:n"; getline(cin, sentence); showValues(sentence, sentence.length()); return 0; } void showValues (string chars, int size) { cout << "nThe string is:"<< endl; for (int index = 0; index < size; index++) cout << chars[index]; cout << endl; // Show how many???? char ch; int vowelCount = 0; int lowerCount=0; int upperCount=0; int spaceCount = 0; int intCount = 0; int punCount = 0; int pos; int x=0; for (pos = 0; pos < size; pos++) { if (chars[pos]) x++; } //space for (pos = 0; pos < size; pos++) { ch=(chars[pos]); switch(ch) { case ' ': spaceCount++; } } //for digits for (pos = 0; pos < size; pos++) { if(isdigit(chars[pos])) intCount++; } //for small caps for (pos = 0; pos < size; pos++) { if(islower(chars[pos])) lowerCount++; } //for big caps for (pos = 0; pos < size; pos++) { if(isupper(chars[pos])) upperCount++; } //for punctuation for (pos = 0; pos < size; pos++) { if(ispunct(chars[pos])) punCount++; } cout << "The number of letter/s and digit/s including the space in the string is: " << x << endl; cout << "nThere are " << spaceCount << " space/s in the string.n"; cout << "nThere are " << intCount << " digit/s in the string.n"; cout << "nThere are " << lowerCount << " small letter/s in the string.n"; cout << "nThere are " << upperCount << " big letter/s in the string.n"; cout << "nThere are " << punCount << " punctuation/s in the string.n"; } |
Fri Feb 18, 2011 3:21 pm by Evildarkwarrior_18
» GRADES for SEMI Finals
Sun Oct 24, 2010 8:26 pm by amvou13
» BSIT 1 Computer Programming 1
Sun Oct 24, 2010 8:11 pm by amvou13
» AIT 1 B Computer Programming 1
Sun Oct 24, 2010 8:09 pm by amvou13
» AIT 1 A Computer Programming 1
Sun Oct 24, 2010 8:05 pm by amvou13
» BSCS 2 Design Analysis of Algorithm
Sun Oct 24, 2010 8:00 pm by amvou13
» ACT 1 Computer Hardware and Software Installation
Sun Oct 24, 2010 7:58 pm by amvou13
» ACT 2 PC Troubleshooting
Sun Oct 24, 2010 7:51 pm by amvou13
» BSCS 3 and BSIT 3 Web Development/Programming 1
Sun Oct 24, 2010 7:49 pm by amvou13