i need little figuring out couple of parts c++ assignment. asked write program follows:
write program accepts input keyboard (with input terminated pressing enter key) , counts number of letters (a-z , a-z), numerical digits (0-9), , other characters. input string using cin , use following looping structure examine each character in string "if" statement , multiple "else if" statements.
char s[50]; int i; . . . = 0; while (s[i] != 0) { // string terminated null (0) value . . . i++; }
your program should make use of relational operators (e.g., == < > <= >= !=) determine whether particular character letter, number, or other character. may #include , may not use other include files.
the program should have output similar following:
enter continuous string of characters no blank spaces (example: abc1234!@#$%
)
enter string: abc1234!@#$%
your string has 12 total characters 3 letters 4 numerical characters 5 other characters
here example program counts lower case letters:
// prog07.cpp example #include <iostream> using namespace std; int main() { char s[50]; int i; int lowercase = 0; //get string user cout << "enter continuous string of characters no blanspaces\n" cout << "(example: abc1234!@#$%)" << endl << endl; cout << "enter string: "; cin >> s; cout << endl; // loop through string, lower case letters // note, strings (character arrays) have invisible // 0 value @ end = 0; while (s[i] != 0) // while character not have ascii code 0 { if ((s[i] >= 'a' && s[i] <= 'z')) lowercase++; i++; } cout << "your string has " << lowercase << " lower case letters" << endl; // including next line dev-c++: system("pause"); // not needed codeblocks return 0; }
so far, have come this:
#include <iostream> using namespace std; int main() { char s[50]; int i; int lowercase, uppercase, numchars, otherchars = 0; cout << "enter continuous string of characters" << endl; cout << "(example: abc1234!@#$%)" << endl; cout << "enter string: "; cin >> s; cout << endl; while (s[i] != 0) // while character not have ascii code 0 { if ((s[i] >= 'a' && s[i] <= 'z')) lowercase++; i++; } while (s[i] != 0) { if ((s[i] >= 'a' && s[i] <= 'z')) uppercase++; i++; } cout << lowercase + uppercase << " letters" << endl; = 0; while (s[i] != 0) { if ((s[i] >= '0' && s[i] <= '9')) numchars++; i++; } cout << numchars << " numerical characters" << endl; return 0; }
any appreciated.
you have reset 0 before every loop:
#include <iostream> using namespace std; int main() { char s[50]; int i; int lowercase, uppercase, numchars, otherchars = 0; cout << "enter continuous string of characters" << endl; cout << "(example: abc1234!@#$%)" << endl; cout << "enter string: "; cin >> s; cout << endl; = 0; //missing while (s[i] != 0) // while character not have ascii code 0 { if ((s[i] >= 'a' && s[i] <= 'z')) lowercase++; i++; } = 0; // missing while (s[i] != 0) { if ((s[i] >= 'a' && s[i] <= 'z')) uppercase++; i++; } cout << lowercase + uppercase << " letters" << endl; = 0; while (s[i] != 0) { if ((s[i] >= '0' && s[i] <= '9')) numchars++; i++; } cout << numchars << " numerical characters" << endl; return 0; }
Comments
Post a Comment